Javascript / jQuery $.get(url) Title Parsing not Working

爷,独闯天下 提交于 2019-12-08 06:37:59

问题


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


回答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?



来源:https://stackoverflow.com/questions/2915795/javascript-jquery-geturl-title-parsing-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!