1

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); } 
3
  • you wrote a function but where it was calling from ? Plus you bind this function into ready function. Why don't you put same attribute in html ? Commented Jan 13, 2017 at 11:13
  • That's the problem, i'm new in JQuery and I've problem to understand how I should call it... Commented Jan 13, 2017 at 11:16
  • 1
    The question is vague, there many possible reasons for this not to work: 1- Navigering.js could return 404. 2- The code has a syntax error as posted in question. 3- You don't need window load inside DOM ready Commented Jan 13, 2017 at 11:16

2 Answers 2

2

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. 
Sign up to request clarification or add additional context in comments.

Comments

0

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; }); }); 

2 Comments

Window load fires after the DOM is completely ready with all images and resources.
Thanks, but it won't work.. I've seen this solution a lot but i don't get it to work for me..