1

Even though the following works: http://jsfiddle.net/N7D5r/

My attempt at using the same code does not correctly get the titles. They return null, for whatever strange reason:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Act on each link
    $('a').each(function(eachIteration){

        // Store current link's url
        var url = $(this).attr("href");

        $.get(url, function(response){
            alert("This is the url: " + url);
            if (((/<title>(.*?)<\/title>/m).exec(response)) != null)
                alert("This would be the title: " + (/<title>(.*?)<\/title>/m).exec(response)[1]);
            else
                alert("title was null");
         });        

    });
});
</script>
<title>test</title>
</head>

<body>
<p><a href="javascript:;">Javascript link</a></p>
<p>&nbsp;<a href="http://www.latentmotion.com">Misc. Link</a> and what else... <a href="http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/#comment-52004762">jQuery Bookmarklet Link</a></p>
<p> Some <a href="#test">anchor link</a> test.</p>
</body>
</html>

Any Ideas?


Note: This question is a follow up on the stack overflow question located here: jQuery Can't $(...).load() the head title in Chrome

Community
  • 1
  • 1
Matrym
  • 16,643
  • 33
  • 95
  • 140
  • How does it not work? What does the whole "response" look like? Does the code say that the title is null, or does it give the wrong result? – Pointy May 26 '10 at 18:55
  • This code works for me, just pasted into a new HTML page. Are you trying to run it locally? If so you may need to allow it to access the external content. – Shawn Steward May 26 '10 at 18:58
  • By it not working, what I mean is that it isn't getting the titles from the links. It's running "Title was null" even when the url does have a title. – Matrym May 26 '10 at 19:03
  • This worked for me in IE8 but didn't work in Chrome, so not sure if you have a compatibility issue here or not. – Shawn Steward May 26 '10 at 19:10

1 Answers1

1

This is the url: /users/recent/205784 This would be the title: User antimatter15 - Recent - Stack Overflow This is the url: /about This would be the title: About - Stack Overflow This is the url: /faq This would be the title: FAQ - Stack Overflow This is the url: /users/205784/antimatter15

Your code seems to work. Could it be that the links are out of the current domain and violating the same-origin-policy?

antimatter15
  • 1,436
  • 1
  • 12
  • 18