Change state of js/jquery with url string? - javascript

I'd like to change the state of the .hide() function based on a URL string or something? Basically I have the following script which shows a sign up form and when you click login the login form displays and the sign up form disapears. I'd like to give a link to current users that shows the login form first. Is there a way to do that with a url string? Or am I stuck?
I have to use js/jquery.
$(function () {
$("#oo-loginContainer").hide();
$(".link1, .link2").bind("click", function () {
$("#form-content, #oo-loginContainer").hide();
if ($(this).attr("class") == "link1"){
$("#form-content").show();
} else {
$("#oo-loginContainer").show();
}
});
});

You absolutely can do this. Many frameworks support the HTML5 history api. History.js is an example.

function getParameterByName(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 decodeURIComponent(results[1].replace(/\+/g, " "));
}
Let's assume that the URL is www.google.com?foo=bar
That would give you:
console.log(getParameterByName('foo')); //bar
Is this what you're asking for?

Related

How to access REAL querystring param in javascript with URL rewrite in place

I am trying to alert the querystring param in jquery because i need that ID on a page load so that i can create some html elements when the control is loaded. I cant seem to access the parameter as the URL rewrite in place and URL looks diff then the original one.
Original URL
www.somesite.com/camping?ProductId=2457&ism=0&cl=PURPLE
URL seen in browser
www.somesite.com/camping/travel-mug-16oz-double-wat-with-adapter-p2457.aspx?ism=0&cl=PURPLE
now, how do i access the ProductId querystring value in Jquery.
Jquery
$(document).ready(function () {
var param = getParameterByName("ProductId");
alert(param) // shows PURPLE as cl in URL is set to cl=PURPLE
});
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
EDIT:
here is the screenshot when i am trying to access the Querysting object on serverside..
Your current code is attempting to access the value from the querystring, which no longer exists as it has been rewritten on the server.
You can use a regular expression to dissect the rewritten URL, no jQuery required. Assuming your productId is always prefaced by -p and is immediately followed by .aspx, then this will work:
var url = 'www.somesite.com/camping/travel-mug-16oz-double-wat-with-adapter-p2457.aspx?ism=0&cl=PURPLE'
var matches = url.match(/-p(\d+)\.aspx/);
var productId = matches && matches[1];
console.log(productId); // = 2457
productId will be null if there are no matches to the regex.
Example fiddle

What should I write in my jQuery function to get the value from my inputfield (id=btnCloseDeal) and display it on another page?

When enter/writing a name in a inputfield (id=btnCloseDeal), and after that I click my GoToPayBtn Im coming to a new page. On my new page I wanna get the value from my inputfield (id=btnCloseDeal) and display it. What should I write in my jQuery function to get the value from my inputfield (id=btnCloseDeal) and display it on another page?
<input type="button" class="artikel" id="btnCloseDeal" value="Pay" onclick="check();";"/>
$("#btnCloseDeal ").click(function (){}
As I understand your question, the way you are thinking of implementing it is not optimal.
I would suggest using your field (#btnCloseDeal) as a query parameter and then fetching it with jQuery.
Here is a function that will get you a query-param in pure JS
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, " "));
}
you can set the value of the input to the url of a page you want to redirect to, then get the url parameter with javascript on that page:
// get the url
var url = window.location.protocol + "//" + window.location.host;
and use:
var param = location.search;

Insert URL parameters into Javascript code

I'm doing some conversion and revenue tracking using Virtual Website Optimizer. To track revenues, they instruct you to add a piece of Javascript code on the thank you page to determine the actual revenue earned.
<script type="text/javascript">
var _vis_opt_revenue = 0;
//zero should be replaced by the actual revenue figure
</script>
I'm using a Wufoo form and made it so that I can add a URL parameter that totals up their order, so for example if their order has a total of $280 they'll be sent to:
http://mysite.com/thank-you?total=280
I'm wondering: how can I make it so that URL parameter is inserted into the Javascript tracking code from Visual Website Optimizer?
Thanks in advance.
Function taken from here
Add this
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, " "));
}
Then change
var _vis_opt_revenue = 0;
to
var _vis_opt_revenue = getParameterByName("total");
Try this
var _vis_opt_revenue =
'$' + window.location.pathname.split('?')[1].split('=')[1];

JavaScript: How to Strip out everything and just get the ID?

I'm creating a Bookmarklet for YouTube, i want to get the ID from the youtube link,
Ex: http://www.youtube.com/watch?v=YgFyi74DVjc
I want only "YgFyi74DVjc" from the above link, that's just an example of what i want, it has to strip out everything and just leave that end part, another thing is
i want to get the ID from this URL as well
http://www.youtube.com/verify_age?next_url=http%3A//www.youtube.com/watch%3Fv%3D[YOUTUBEID]
So basically when you click on some video and then on this bookmarklet it gets the youtube video ID and redirects them to a link which unlocks the video or expands the video to full size of the user's browser, i have everything coded and just need a way to get ID's from both the links, i have coded this which works for the 2nd link
var url = "http://www.youtube.com/verify_age?next_url=http%3A//www.youtube.com/watch%3Fv%3D[YOUTUBEID]";
var ytcode = url.substr(80,50);
alert(ytcode);
Now i want someway to get ID's from both the links, please help!
This is a job for regex:
url.match(/(\?|&)v=([^&]+)/).pop()
which, broken down, means:
url.match(
// look for "v=", preceded by either a "?" or a "&",
// and get the rest of the string until you hit the
// end or an "&"
/(\?|&)v=([^&]+)/
)
// that gives you an array like ["?v=YgFyi74DVjc", "?", "YgFyi74DVjc"];
// take the last element
.pop()
You can use this for the second form as well if you decode it first:
url = decodeURIComponent(url);
The url variable now equals "http://www.youtube.com/verify_age?next_url=http://www.youtube.com/watch?v=[YOUTUBEID]", and the regex should work.
You could put it all together in a reusable function:
function getYouTubeID(url) {
return decodeURIComponent(url)
.match(/(\?|&)v=([^&]+)/)
.pop();
}
Assuming the ID (i.e. the v parameter) is always the last parameter in the URL:
url = url.replace("%3D", "=");
var splitUrl = url.split("=");
var yId = splitUrl[splitUrl.length - 1];
The replace function is used to replace the character code %3D with =, which is what it represents. You can then split the string on the = character, and your ID will be the last element in the resulting array.
You could use this function (just pass it the url and the name of the parameter)
function getParameterByName( name, url )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( url );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
var url = "http://www.youtube.com/watch?v=YgFyi74DVjc"
var v= getParameterByName('v', url);
Slight modification to nrabinowitz answer to allow for YouTube share URLs such as:
http://youtu.be/3Ah8EwyeUho
Also i have attached it to a class so it can be easily reused.
$(function(){
$('.js-youtube').on('blur', function(){
var newval = '';
if (newval = $(this).val().match(/(\?|&)v=([^&#]+)/)) {
$(this).val(newval.pop());
} else if (newval = $(this).val().match(/(youtu\.be\/)+([^\/]+)/)) {
$(this).val(newval.pop());
}
});
});

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