Cannout set selected item jquery - javascript

I'm taking values from the url using the following function.
function getParameterByName( name ) //courtesy Artem
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
The url is as following.
http://localhost:9157/layout?oc=ARES&sn=Charitha&on=Araliya%20Restaurant.
This is the code where I try to set the element to be selected.
var oc = getParameterByName('oc');
$("#" + oc).prop("selected", true);
No matter how I checked using firebug and other debugging tools, the element doesn't get selected! is there anything that I'm doing wrong here! I checked whether the value from the getParameterByName() method returns correct. The values are correct! bt it doesn't work.

Try this:-
$("#my-Select option[text=" + oc +"]").attr("selected","selected") ;

Related

extract value from href tag with jquery

Url is generated like this
/Home/LoadData?page=2&activeTab=House
How can I grab activeTab value?
function getURLParameter(name) {
return decodeURI(
(RegExp(name+ '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
and call it as getURLParameter('activeTab');
From Here
please add this function in your javascript
and you can pass param name and you can get value
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
hope it's help for you .
You can use split
Live Demo
url.split('activeTab=')[1]
You can use javascript's substr() and .lastIndexOf():
var url = '/Home/LoadData?page=2&activeTab=House'; // window.location.href;
var activeTab = url.substr(url.lastIndexOf('=')+1); // outputs House
Find in FIDDLE
try something like this
$(document).ready(function(){
var url = 'http://stackoverflow.com/Home/LoadData?page=2&activeTab=House';
alert(decodeURI(
(RegExp('activeTab=' + '(.+?)(&|$)').exec(url)||[,null])[1]
));
});

Get a parameter from a rewritten URL with javascript

I am trying to get a url parameter using javascript so i can pass the paramter to google maps
The problem is i'm using mod rewrite on the url
www.mysite.com/1/my-event
instead of
www.mysite.com/mypage.php?id=1&name=my-event
I've tried doing an alert but it comes up blank
Here is the javascript function that will work if i don't rewrite the url
function gup( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
The rewritten format, with the query-string, isn't available to your JavaScript.
You'll have to grab the value out of location.pathname (/1/my-event in your example), instead:
var params = window.location.pathname.split('/').slice(1); // ["1", "my-event"]
var id = params[0];
var name = params[1];
Just split the URL on / characters and take the last elements in the resulting array, mapping them to the names you expect.

How do I extract "search term" from URLs?

How do I extract "test" from the following URL?
http://www.example.com/index.php?q=test&=Go
I've found the a script to extract the path (window.location.pathname), but I can't find or figure out how to extract or split the URL.
-ben
var m = window.location.search.match(/q=([^&]*)/);
if (m) {
alert(m[1]); // => alerts "test"
}
var myURL = 'http://www.example.com/index.php?q=test&=Go';
function gup( name ) //stands for get url param
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( myURL );
if( results == null )
return "";
else
return results[1];
}
var my_param = gup( 'q' );
Here is the jsfiddle
Or you can use jQuery's plugin:
URL Parser JQuery
If you just want the value of the first term, then:
function getFirstSeachValue() {
var s = window.location.search.split('&');
return s[0].split('=')[1];
}
If you want the value of the 'q' term, regardless of where it is in the search string, then the following returns the value of the passed term or null if the term isn't in the search string:
function getSearchValue(value) {
var a = window.location.search.replace(/^\?/,'').split('&');
var re = new RegExp('^' + value + '=');
var i = a.length;
while (i--) {
if (re.test(a[i])) return a[i].split('=')[1];
}
return null;
}
Both are just examples of course and should test results along the way to prevent unexpected errors.
--
Rob

How can I get URL parameter in a jquery ready function?

I have the following:
$(document).ready(function() {
window.location.href='http://target.SchoolID/set?text=';
});
So if someone comes to a page with the above mentioned code using a url like:
Somepage.php?id=abc123
I want the text variable in the ready function to read: abc123
Any ideas?
you don't need jQuery. you can do this with plain JS
function getParameterByName( name ) //courtesy Artem
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
and then you can just get the querystring value like this:
alert(getParameterByName('text'));
also look here for other ways and plugins: How can I get query string values in JavaScript?
check out the answer to this questions:
How can I get query string values in JavaScript?
that'll give you the value of the id parameter from the query string.
then you can just do something like:
$(document).ready(function() {
var theId = getParameterByName( id)
var newPath = 'http://target.SchoolID/set?text=' + theId
window.location.href=newPath;
});
To help people coming after me that want to have multiple variables and to enhance Moin's answer here is the code for multiple variables:
function getParameterByName( name ) //courtesy Artem
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
{
if ((results[1].indexOf('?'))>0)
return decodeURIComponent(results[1].substring(0,results[1].indexOf('?')).replace(/\+/g, " "));
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
}

url parameter to save is in JavaScript

I would like to insert a parameter value found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123
Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code below:
var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/**NUMBER OF URL HERE**/goal");
Can someone help me please?
You need to parse the querystring. Javascript has no inbuilt way of doing this easily, but there are some nice functions you can use:
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
http://www.netlobo.com/url_query_string_javascript.html
Then in your example you could do:
var queryData = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3"); gwoTracker._trackPageview("/" + queryData + "/goal");

Categories

Resources