开发者

how can i grab a text from a href in a website (page source) using javaScript?

开发者 https://www.devze.com 2022-12-16 16:53 出处:网络
This is the part of page source of a website. I need to grab the \" crsEdId=78 \" in the last line. Using JavaScript or JQu开发者_C百科ery.

This is the part of page source of a website. I need to grab the " crsEdId=78 " in the last line. Using JavaScript or JQu开发者_C百科ery.

I will use mozilla Jetpack to do the task.

How can i Get this Code first? and What to do (RegEx or JQuery) on it !?

<div class="tools-menu-body" id="tools-menu-div" style="display: none;">
        <div id="menu_glider">
            <div class="scroller">
                <div class="content">
                     <div class="section" id="courses_menu">
                        <ul class="tools-menu-ul">
                            <li class="tools-menu-back"><a href="#main_menu" class="controls">back</a></li>
<li><a title="Advanced Software Engineering Lab: Open Source Development" href="../Courses/CourseEdition.aspx?crsEdId=78">CSEN 914 </a></li>

IT WORKED :

well it was a stupid thing - i just had to put the selector inside the click function, donno why i didn't think about it earlier.

  jetpack.statusBar.append({
  html: "Boom<i>!</i>",
  width: 45,
  onReady: function(widget){
      $(widget).click(function(){
             var doc= jetpack.tabs.focused.contentDocument;
             var link= doc.querySelector('#courses_menu > ul > li:nth-child(2) a');
             var test= doc.getElementById('#container'); 
             jetpack.notifications.show("Course is is = "+ link);

});
}
});    

Now i need to get all the href inside this courses_menu !! Can you help in that ?


Maybe I'm missing something, but can't you just get the href for that element, then use RegEx to get what you want?


Try:

alert($('#courses_menu').children('li').get(2).attr('href'));


In jQuery:

var matches = $('a').attr('href').match(/crsEdId=(\d+)/);


If you've got the document in the current tab, I believe Jetpack gives it to you like:

var doc= jetpack.tabs.focused.contentDocument;

Then in plain JS, get hold of the link by whatever means is most appropriate. For example to get the first link from the second list item:

var menu= doc.getElementById('courses_menu');
var link= menu.getElementsByTagName('li')[1].getElementsByTagName('a')[0];

If you are using Jetpack you can presumably rely on a new enough version of Firefox to have native support for the Selectors-API, so you could get this more easily without the use of an additional library such as jQuery, eg.:

var link= doc.querySelector('#courses_menu>ul>li:nth-child(2) a');

Now old-school DOM Level 0 gives you several easy properties to get the parts of a URL inside a link without having to resort to unreliable regex processing. If you want the whole query string crsEdId=78, you can say:

var query= link.search.substring(1);

(the substring is to take off any leading ? character.)

If there might be more query parameters and you want to specifically find the one called crsEdId you'd have to process the query fully, eg.:

var crsEdId= getParameter(query, 'crsEdId');

function getParameter(query, name) {
    var pars= query.split(/[&;]/g);
    for (var i= pars.length; i-->0;) {
        var n= pars[i].split('=')[0];
        if (decodeURIComponent(n)===name)
            return decodeURIComponent(pars[i].substring(n.length+1));
    }
}
0

精彩评论

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