/ Published in: JavaScript
Pass variables into javascript files using query strings, and return them using getJSvars()
Example
//HTML
//return the value of "foo"
getJSvars('filename.js', 'foo');
Example
//HTML
//return the value of "foo"
getJSvars('filename.js', 'foo');
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function getJSvars(script_name, var_name, if_empty) { var script_elements = document.getElementsByTagName('script'); if(if_empty == null) {var if_empty = '';} for (a = 0; a < script_elements.length; a++) { var source_string = script_elements[a].src; if(source_string.indexOf(script_name)>=0) { var_name = var_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regex_string = new RegExp("[\\?&]"+var_name+"=([^&#]*)"); var parsed_vars = regex_string.exec(source_string); if(parsed_vars == null) { return if_empty; } else { return parsed_vars[1]; } } } }
URL: http://posheika.net/?p=42