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

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..

Related

How can i change the variable name in javascript? [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed last year.
I have some js code like this:
var parameter0 = 12345;
var parameter1 = 54321;
var parameter2 = 33333;
var parameter3 = 99990;
function selectValue(number) {
alert(parameter+number);
}
selectValue(2);
Here is a fiddle - https://jsfiddle.net/frwd2qLg/
This code will not work, because, for example, for number = 2, it will not show 33333, but will be undefined. Any workaround?
As people said in your comments, you could use an array or an object to do this task. But answering your question you culd use an eval to access the variable name.
var parameter0 = 12345;
var parameter1 = 54321;
var parameter2 = 33333;
var parameter3 = 99990;
function selectValue(number) {
alert(eval("parameter"+number));
}
selectValue(2);

Variable in a variable name? Looping said variables? [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 5 years ago.
So I have a chunk of code... and I want to make it more efficient by condensing it to a few lines instead of twelve. The idea I had was to use the variable of a loop to call each variable in sequence, since the code is just repeated with different numbers each time. Is there any way this could work?
var usetext1 = getText("text1");
var usetext2 = getText("text2");
var usetext3 = getText("text3");
var usetext4 = getText("text4");
var usetext5 = getText("text5");
var usetext6 = getText("text6");
usetext1 = usetext1.toUpperCase();
usetext2 = usetext2.toLowerCase();
usetext3 = usetext3.toLowerCase();
usetext4 = usetext4.toLowerCase();
usetext5 = usetext5.toLowerCase();
usetext6 = usetext6.toLowerCase();
Reduced to something like:
for (var i=2;i<6;i++){
var usetext[i]=getText("text[i]");
usetext[i]=usetext[i].toLowerCase();
You can use Template Literals to store the value into an array
var arr = [];
for (var i=1; i <= 6; i++){
arr.push(getText(`text${i}`).toLowerCase());
}

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.

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 - how to retrieve the src="" url from the script contents? [duplicate]

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.

Categories