fmz 14

I have a dynamic navigation and need to open one link in a new window.

As such I am using jQuery to help, but the following code does not seem to work:

<script type="text/javascript">
$('a[href="http://globalhealth-dev.oit.duke.edu/education/global-health-courses"]')
.attr("target", "_blank");
</script>

To try it for yourself: http://globalhealth-dev.oit.duke.edu/education/ then click on the Global Health Courses link under Education.

I would appreciate some help getting this to work properly.

Thanks.

  • This is the sort of thing that you'd want to put on StackOverflow.com for better results. This is a programming question, not a design question :) Matchu almost 7 years ago

4 answers

Jordan 469
0
points
This was chosen as the best answer

You are missing a command line. You need to add the following to the top of the code $(document).ready(function(){ . So your full code should be

$(document).ready(function(){
    $("a[@href^='http']").attr('target','_blank');
});
Answered almost 7 years ago by Jordan
  • The "@" symbol in the selector was deprecated in jQuery... just remove it, so it should look like this: $("a[href^='http']) Mottie almost 7 years ago
fmz 14
1
point

I got a little help from our friends at Stack Overflow and they provided this:

<script type="text/javascript">
$(document).ready(function() {
    $('a[href="/education/global-health-courses"]').attr("target", "_blank");    
});
</script>
Answered almost 7 years ago by fmz
0
points

Hi, If you are looking for a solution XHTML-compatible you must use the rel attribute of the <a> tag.

<a href="http://YOUR_LINK_URL_HERE" rel="external">

<script>
/* at end of the page  */
$('a[rel=external]').click(function() { 
    window.open(this.href); 
    return false; 
});  

Answered almost 7 years ago by joao.parana
-1
points

It may also be beneficial to you to use the following (if your links do not exist on page load)

<script type="text/javascript"> $(document).live(function() { $("a[@href^='http']").attr('target','_blank'); }); </script>

This will apply to all current and future matching elements on a page.

You can learn more here: jQuery .live

Answered almost 7 years ago by Jason Michael
  • That is most definitely an improper usage of the live method. Matchu almost 7 years ago
  • Also use of the "@" symbol in the selector was deprecated in jQuery v1.2 Mottie almost 7 years ago