views:

114

answers:

4

This is the part of page source of a website. I need to grab the " crsEdId=78 " in the last line. Using JavaScript or JQuery.

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 ?

+1  A: 

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

brian
I am kinda new in JavaScript and RegEx , Can you show me how would this work !?or link me to it !
msheshtawy
I was thinking something along the lines of what andybaird posted. But you may need to be more specific in the selector.
brian
A: 

In jQuery:

var matches = $('a').attr('href').match(/crsEdId=(\d+)/);
Andy Baird
Jetpack outputs $("a").attr("href") is undefined
msheshtawy
A: 

Try:

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

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));
    }
}
bobince
it is Giving me null !! here is the website iam tryin to work on http://met.guc.edu.eg/and thats the code iam trying to do var doc= jetpack.tabs.focused.contentDocument;var link= doc.querySelector('#courses_menu>ul>li>li:nth-child(2) a'); jetpack.statusBar.append({ html: "Boom<i>!</i>", width: 45, onReady: function(widget){$(widget).click(function(){ jetpack.notifications.show( " "+link+" " ); }); }});
msheshtawy
There seems to be a spurious extra `>li` in that selector.
bobince
yeah , i was just trying something - .. well with and without it , its still giving me null
msheshtawy
Where exactly are you getting the null? Is `link` null? Can you get `#courses_menu` on its own? Which part is failing here?
bobince
The link is giving me null , iam printing the link , i will try to debug it , ( am kinda new in JS )
msheshtawy
Post some example code? The selector certainly *should* match the HTML given above.
bobince
I updated the post , take a look. Thanks a lot for you time and effort :)
msheshtawy
You're getting `main_menu`; there's no element with that id; you want `courses_menu`.
bobince
Still gives me null !! am readying the JQuery documentation now, maybe ill find something Thanks alot Bobince
msheshtawy
Incidentally there's an additional problem with the `getElementById` call; you shouldn't include the selector-style `#` before the ID in this method. ie. should be `doc.getElementById('courses_menu');`.
bobince