0

I'm using a library which has already defined an onclick event handler on a hyperlink eg:

<a onclick="$('#myDiv').load('/?__=634285190817664832&sort=Id&sortdir=ASC&page=1 #myDiv');" href="#">1</a>

How can I get the value of the 'url' parameter?

2
  • Regex on what exactly? The DOM? I can access the onclick attribute of the href, and I get: function onclick(event) { $("#grid").load("/?page=3&__=634285215353428198 #grid"); } Commented Dec 21, 2010 at 9:44
  • OK - Regex on the returned value of the onclick attribute, taking into account TJ Crowders warnings. Thanks. Commented Dec 21, 2010 at 10:01

3 Answers 3

1

Updated Answer

...after tobyodavies pointed out (in the nicest of ways) that I was being thick. If you explicitly retrieve the attribute, rather than the reflected property, we'll get back the string from the DOM, not a function, and so don't have to worry about function decompilation (see notes below). The remainder of the below is a bit paranoid (because I originally was working from function decompilation), but still:

jQuery(function($) {

  var link, onclick, url, index;

  link = $('#theLink')[0];
  // IMPORTANT: Getting the *attribute* here, not the reflected property.
  // jQuery's `attr` function will give you the property, so go direct.
  onclick = link.getAttribute("onclick");
  display("onclick = " + onclick);
  index = onclick.indexOf("load('");
  if (index < 0) {
    url = "(unknown)";
  }
  else {
    url = onclick.substring(index + 6);
    index = url.indexOf("');");
    if (index > 0) {
      url = url.substring(0, index);
    }
  }
  display("url = " + url);

  function display(msg) {
    $("<p/>").html(msg).appendTo(document.body);
  }
});​

Live example

Original Answer

Note that here there be dragons. The value of the onclick reflected property by the time you're accessing it in the DOM is a function object on most browsers, and so you'll have to use Function#toString, which has never been standardized and some mobile browsers are known to just return "[object Function]". Most desktop browsers return a decompiled version of the event handler, but that can change at any time.

But now you're aware of the dragons, you can get it that way. Here's a somewhat paranoid approach:

jQuery(function($) {

  var link, onclick, url, index;

  link = $('#theLink')[0];
  onclick = "" + link.onclick;
  display("onclick = " + onclick);
  index = onclick.indexOf("load('");
  if (index < 0) {
    url = "(unknown)";
  }
  else {
    url = onclick.substring(index + 6);
    index = url.indexOf("');");
    if (index > 0) {
      url = url.substring(0, index);
    }
  }
  display("url = " + url);

  function display(msg) {
    $("<p/>").html(msg).appendTo(document.body);
  }
});​

Live example

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not bothered about the dragons, and thanks for the effort you put in.
why not use the attribute nodes in the DOM - no parsing the string representation of the function?
@tobyodavies: Gah, I keep forgetting that the attribute is still there and may have a different (more useful!) value than the reflected property. Nice one. Fixed!
0

Try this:

<a href="http://google.de" onclick="alert(this.href);">Link</a>

Or did you mean this?

$(function() {
    var a = $('#link').attr('onclick');
    var l = (a+"").split("'");
    alert(l[3]);
});

You can also replace the "$" object (Beware! This is solution is working in chome, but I did not do too much testing...)

$(function() {
    var a = $('#link').attr('onclick');
    var temp = $;

    // Override jQuery
    $ = function (t) {
        var s = new Object;
        s.load = function(url) { alert(url); }; 
        return s
    }
    a();

    // ...and back again
    $ = temp;

    // Is jQuery still working?
    alert($('#link').text());
});

2 Comments

jQuery abuse alert! alert($(this).attr('href')); => alert(this.href); ;-) More seriously, though, not seeing how this answers the OP's question about extracting the URL from the load call...
The href value is effectively hardcoded as #. That's not what I want. I want the value of the url parameter in the jQuery function.
0

Try using the attribute nodes to get the un-parsed version, then use a regex to extract the URL.

link.attributes.getNamedItem('onclick').value.match(/someRegex/)

1 Comment

That will get the href of the link, though, not the URL that the load function in the handler is using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.