开发者

Running the javascript code present on another html page through jquery

开发者 https://www.devze.com 2023-04-06 02:30 出处:网络
It might be a noob questions but I have just started using jquery. My basic requirement to extract the link which is there in the javascript code present in another html (code is embedded in the html

It might be a noob questions but I have just started using jquery. My basic requirement to extract the link which is there in the javascript code present in another html (code is embedded in the html page and not in a seperate file).

The link is also present as a href attribute of <a> tag inside a tag, just to add if it is easier to extract it from there (I am using chrome so I think it considers there are no child nodes of <noscript> tag)

After this I tried doing an ajax request to the html page (using $.ajax) thinking it will run the scripts on the page but got the html code of the page in return :S . I have also heard of something called evalscripts:true but not sure if that will work here or how to use it?

I have also tried to search for the link in html code returned by my html page by using the "contains" operation of jquery.

I am doing all this to create a greasemonkey script. Please suggest

Example Code: This is a function present inside the html of that page:

function fun() {
    obj = new pollingObj('argument', "a link I want to extract comes here");
}

I want to开发者_StackOverflow社区 extract the link: "a link I want to extract comes here" and then open it.on my page where I am running my jquery script

This link is also present like this on the html page:

<noscript>
   <a href = "a link I want to extract comes here" target="_blank">blabla</a>
</noscript>

Also is it possible to run the javascripts present on that page if the link extraction is not possible?


If you're able to get the html code of the page successfully via .ajax, and the data you want is in the HTML code, it's not worth the effort to bother with trying to run the scripts. Just access the URL through the DOM:

// ajax success function
success: function(html) {
    var anchorCode = $(html)
        // this assumes that noscript is a top-level element
        // otherwise, use .find('noscript')
        .filter('noscript')
        .text(); // get the code for the anchor tag, as a string
    var myLink = $(anchorCode).attr('href');
    // do something with myLink
}

Edit: It turns out that jQuery is a little funny in the way it deals with noscript tags - inner tags don't appear to be considered part of the DOM, so you need to grab the text content of the tag and then use jQuery to DOM-ify it. See updated code above.

0

精彩评论

暂无评论...
验证码 换一张
取 消