user7302419
user7302419

Reputation:

Open new window from link with JQuery

I've found some ways that should work in this forum, but it won't work for me. I to press on a link and then a new window should open in a new window.

My HTML-code is:

  <!DOCTYPE html>
  <html lang="sv-se">
  <head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="Navigering.js"></script>
    <title>Navigering</title>
</head>
<html>
<body>
<h1>Navigering</h1>
<a href="https://www.google.se/" alt="google">Link toGoogle</a>
</body>

And my JQuery-code:

$(document).ready(function(){
 function myFunction() {
  $("a").attr('target','_blank');
 }
$(window).load(myFunction);
}

Upvotes: 1

Views: 3963

Answers (2)

dibiler
dibiler

Reputation: 11

The code should be:

$(document).ready(function(){
  $("a").attr('target','_blank');
}

Edit:

If this solution isn't working for you, may it be possible that you are loading the links afterwards dynamically? because in that case this script won't detect the new added links and they won't be modified. In that case you could use the following:

$(document).ready(function(){
    $('document').on('click','a', function() {
      window.open( $(this).attr('href') );
      return false;
    });
});

Upvotes: 0

Anushri_Systematix
Anushri_Systematix

Reputation: 66

For this please try below code :

<!DOCTYPE html>
<html lang="sv-se">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="Navigering.js"></script>
<title>Navigering</title>
</head>
<body>
<h1>Navigering</h1>
<a href="https://www.google.se/" onclick="window.open(this.href, 'newwindow', 'width=300, height=250'); return false;" alt="google">Link toGoogle</a>
</body>
</html>

Here, If we click on "LinktoGoogle" link then it will open link in new window (also we can set height and width of new window) and there is no need to add any jquery code for this.

Upvotes: 2

Related Questions