views:

196

answers:

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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"&gt;&lt;/script&gt;
<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"&gt;Misc. Link</a> and what else... <a href="http://www.latentmotion.com/how-to-create-a-jquery-bookmarklet/#comment-52004762"&gt;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: http://stackoverflow.com/questions/2911930/jquery-cant-load-the-head-title-in-chrome

+1  A: 

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
Same domain, as tested here: http://latentmotion.com/bookmarklets/link-checker/test.html
Matrym
DOH! It was a no www subdomain issue I think.
Matrym