开发者

jQuery: Get value of url in load() function

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?


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


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());
});


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/)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜