JavaScript - how to retrieve the src="" url from the script contents? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript query string
I'd like to access query variables attached to my script url. So for example:
<script type="text/javascript" src="http://mysite.com/app.js?var1=value1&var2=value2"></script>
In app.js, how do I access the var1 and var2 values?

This page describes a method for getting these values:
<script type="text/javascript"
src="scriptaculous.js?load=effects,builder"></script>
And the javascript:
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]; }
}
}
}

Parse the src attribute, roughly as follows:
var src=document.getELementById("script-id").getAttribute("src");
var query=src.substring(src.indexOf("?"));
var query_vals=query.split("&");
var queries={};
for (var i=0;i<query_vals.length;i++) {
var name_val=query_vals.split("=");
queries[name_val[0]]=name_val[1];
}
console.log(queries.var1, queries.var2);
However, there are libraries such as url.js which are a better bet for doing this, and much more robust in the face of URL encoding etc.

Related

JSON parsing syntax impossible [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to rotate a DIV, but that is not the problem I am facing, it is JSON parsing,
I want to be able to get the proper attr value from the variable rotateAttrSet but I want to get it according to the browser type
I can do var rotateAttr = rSET.FF;, but I can't do var rotateAttr = rSET.brwShort;
Is there a way to make this work?
again, I am not looking for ways to rotate the DIV, I just want to know if there is a way to get the JSON work by a variable (like .brwShort below)
Thanks
<script>
var rotateAttrSet = '{"IE":"-ms-transform","FF":"-moz-transform","CR":"-webkit-transform","SF":"-webkit-transform","OP":"-o-transform","WC3":"transform"}';
function rotator(o)
{
var o = $(o);
var angle = 0;
var rSET = parseJSON(rotateAttrSet);
var brwShort = "FF";//getBrowser().split(";")[2];
var rotateAttr = rSET.brwShort;
//alert(rotateAttr);
//o.removeAttr("onClick");
setInterval(function(){
angle++;
if(angle == 360) angle = 0;
o.text(angle);
o.css(rotateAttr, "rotate("+angle+"deg)");
}, 10);
}
function parseJSON(s)
{
return eval('('+s+')');
}
</script>
You need to use the browser short as a key as follows:
var brwShort = "FF";//getBrowser().split(";")[2];
var rotateAttr = rSET[brwShort];
Otherwise, it is actually looking for a property on the object with a key of "brwShort", which doesn't exist on your object.

Javascript / jquery - like PHPs $_GET [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
I have a variable set in my url:
http://mydomain.com?myvar=1
Now I need to check this when the page loads. I would normally do this in PHP but I need to use some jQuery. What I want to do is something like:
$(document).ready(function() {
var somevar = $GET['myvar'];
if (somevar = '1') {
$('#someDiv').hide();
}
});
How can I do this if its at all posible?
Here's a function that will do that:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

Parse variables from querystring in javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I get query string values?
I have the following querystring:
"active_tab=delivered&active_tab=all&active_tab=delivered&active_tab=outstanding
&active_tab=delivered&active_tab=outstanding&active_tab=all&active_tab=delivered&active_tab=outstanding&title_filter=conformance&title_filter=delivering&title_filter=packaging
&title_filter=delivering&title_filter=all&title_filter=delivering&title_filter=all&title_filter=packaging&title_filter=conformance&title_filter=packaging
&title_filter=delivering&title_filter=packaging&title_filter=ordered"
How would I parse the final title_filter ("ordered") and active_tab ("delivered") from the above querystring? Also, if that querystring variable doesn't exist, have it = ""
var query = {};
var largeString = "active_tab=delivered&active_tab=all&active_tab=delivered&active_tab=outstanding&active_tab=delivered&active_tab=outstanding&active_tab=all&active_tab=delivered&active_tab=outstanding&title_filter=conformance&title_filter=delivering&title_filter=packaging&title_filter=delivering&title_filter=all&title_filter=delivering&title_filter=all&title_filter=packaging&title_filter=conformance&title_filter=packaging&title_filter=delivering&title_filter=packaging&title_filter=ordered";
largeString.split('&').forEach(function(keyValue){
var kvp = p.split('=');
query[kvp[0]]= kvp[1];
});
if you need to support arrays:
largeString.split('&').forEach(function(keyValue){
var kvp = keyValue.split('=');
if(kvp[0] in query){
if(typeof(query[kvp[0]] === 'string')){
query[kvp[0]] = [query[kvp[0]]];
}
query[kvp[0]].push(kvp[1]);
}else{
query[kvp[0]] = kvp[1];
}
});
I modified the querystring to remove duplicates and then I did:
var active_tab = window.location.search.split('active_tab=')[1].split('&')[0]
var title_filter = window.location.search.split('title_filter=')[1].split('&')[0]

Javascript Regular expression test method no working properly [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why RegExp with global flag in Javascript give wrong results?
I have the following method to which I'm passing these parameters:
var stringValue = "50, abc";
var stringArray = stringValue.split(",");
var agePattern = /^([0-9]|[1-9][0-9]|[1][0-4][0-9]|[1][5][0])$/g;
age = getMatchingString(stringArray, agePattern);
//---------------------------------------------
function getMatchingString(stringArray, regexPattern) {
//alert("getMatchingString");
for (var i=0; i < stringArray.length; i++) {
if (regexPattern.test(stringArray[i])) {
return (stringArray[i].match(regexPattern)).toString();
}
}
return null;
}
Chrome shows the following funny behavior where test method with stringArray[i] and stringArray[0] show different values even when i = 0 as shown in the image:
Can someone explain this to me please?
var stringValue = "50, abc";
var stringArray = stringValue.split(",");
var age = getMatchingString(stringArray);
function getMatchingString(stringArray)
{
var len=stringArray.length;
for (var i=0; i < len; i++)
{
if(!isNaN(stringArray[i]))
{
return stringArray[i]
}
}
return null;
}
alert(age)//50
Perhaps, if you are looping through the array to check for the existence of a numeric value as such age, isNaN is much better a option to use here than using a regex pattern.
DEMO

How to extract a GET parameter from the URL in Javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use the get parameter of the url in javascript
Suppose I have this url:
s = 'http://mydomain.com/?q=microsoft&p=next'
In this case, how do I extract "microsoft" from the string?
I know that in python, it would be:
new_s = s[s.find('?q=')+len('?q='):s.find('&',s.find('?q='))]
I use the parseUri library available here:
http://stevenlevithan.com/demo/parseuri/js/
It allows you to do exactly what you are asking for:
var uri = 'http://mydomain.com/?q=microsoft&p=next';
var q = uri.queryKey['q'];
// q = 'microsoft'
(function(){
var url = 'http://mydomain.com/?q=microsoft&p=next'
var s = url.search.substring(1).split('&');
if(!s.length) return;
window.GET = {};
for(var i = 0; i < s.length; i++) {
var parts = s[i].split('=');
GET[unescape(parts[0])] = unescape(parts[1]);
}
}())
Think this will work..

Categories