Hi all, i get a longString from object's onclick value

var longString = String(this.onclick);

output like below;

function onclick(event) { window.location.href = "index.html?q1=v1&g2=v2"; }

i want parse that like below;


how can i do this with pure js or jquery which works with all browser?

Thank you for your help, already now.

Regards Kerberos

+1  A: 

parseUri is what you are looking for.

+1  A: 

Everything is here :

Google is your friend mate.

Guillaume Lebourgeois
+1  A: 

is this what you are looking for

var str = 'function onclick(event) { window.location.href = "index.html?q1=v1&g2=v2"; }';
alert( /".+?"/.exec(str) );

The following will extract the first match from any one line function (regardless of the name or spacing):

var input = "function onclick(event) { window.location.href = \"index.html?q1=v1&g2=v2\"; }";
var pattern = /function\s+\w+\(\w*\)\s*{\s*window.location.href\s*=\s*['"]([^'"]*)['"];\s*}/i;
var output = pattern.exec(input);

After this runs, the index.html... part will be in output[1]


This function doesn't do any parsing, but instead declares its own window.location.href, evals the given code, so the onclick becomes a defined function within the scope of this function, runs it, so our local window.location.href gets updated and then returns it. It assumes the contents of the string are known, safe and similar to what you've already given.

function extractURL(code) {
    var window = { location: { href: '' } };
    return window.location.href;

Example use,

var fn = 'function onclick(event) { window.location.href = "index.html?q1=v1&g2=v2"; }';
extractURL(fn); // "index.html?q1=v1&g2=v2"
+1  A: 

You can grab it using a regex like this:

var string = 'function onclick(event) { window.location.href = "index.html?q1=v1&g2=v2"; }';
var url = string.match(/"(.*?)"/)[1];
alert(url); // == index.html?q1=v1&g2=v2, without quotes​​​​​​​​​​

You can try a demo here, be aware that the regex returns an object, to get the actual string you're probably after the second entry, [1] here.

Nick Craver
thank you this code works great.
+1  A: 

Here's one way to do it if you don't like regular expressions.

var str = 'function onclick(event) { window.location.href = "index.html?q1=v1&g2=v2"; }';
var idx = str.indexOf('"')+1;
var res = str.substr(idx, str.lastIndexOf('"') - idx );

Try it out:

patrick dw
thank you. this code works with ff but does'nt work with ie. Actuallly firstly i get this string from onclick like this var longString = String(this.onclick); after that i want to parse it.
@Kerberos - Which version of IE? Works for me just fine. And are you saying that the string actually starts with `"window.location..."`? It should still work.
patrick dw
but i get this string value from object onclick value when codes run. i don't manipulate like i wrote. i edited my post. may be that clear more.
@Kerberos - I don't get it. Are you getting the `onclick` of the element that was clicked? Either way, should work. Could you post the actual code so I can see what you mean? Here's another example:
patrick dw
Patrick, OK. i found problem's source. FF output to string onclick value with " character but ie output to string with ' character. when replace to " to ' problem solved.Thank you very much for your help. regards kerberos.