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;
}
Related
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);
This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 7 years ago.
I have a input hidden type which specifies the function that needs to be called. How can I do it using Javascript/Jquery
<input type="hidden" name="extrafunction" id="extrafunction" value="edit_data_provider/DataProviderChange1" />
and in Javascript file
var fullfunctionName = $('#extrafunction').val();
var control_name = fullfunctionName.split('/')[0];
var function_name = fullfunctionName.split('/')[1];
if(control_name == client_control_name)
{
//The function call which is in function_name var should come here
}
You can use :
window[functionName](arguments);
Hope this helps.
var functionName="my_function";
function my_function(){
console.log('test from my_function');
}
window[functionName]();
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I have an url like below
http://www.grcparfum.it/home.php?section=letteradelpresidente&lang=eng
in this URL language is english
lang=eng
i wanna call different JS file when lang is different
Here's a function that can get the querystring variables in JS for you:
function $_GET(q){
var $_GET = {};
if(document.location.toString().indexOf('?') !== -1){
var query = document.location
.toString()
.replace(/^.*?\?/, '')//Get the query string
.replace(/#.*$/, '')//and remove any existing hash string
.split('&');
for(var i=0, l=query.length; i<l; i++){
var aux = decodeURIComponent(query[i]).split('=');
$_GET[aux[0]] = aux[1];
}
}
return $_GET[q];
}
Does this help at all?
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]
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.