Link to home
Start Free TrialLog in
Avatar of MacLean Fitzgerald
MacLean FitzgeraldFlag for United States of America

asked on

JS: getURL() and href

I recently asked a question and received a great solution.  

See here for reference:

https://www.experts-exchange.com/questions/28369086/JS-getURL-and-href.html?anchorAnswerId=39910149#a39910149

One problem.  Im getting a conflict of sorts when I try to apply the solution to multiple instances.  For example, if I just use it by itself the solution works.  It uses getURL and all is good.  When I try to copy it and add a new one.  In this case getURLHelp, and change all the getURL to getURLHelp it breaks it.  I'm wondering what I need to to change to make it work for different instances.

Ideally, I would have it for the following:

getURL
getURLHelp
getURLAccount
getURLGift

Here is the solution from the above referenced question:

<script type="text/javascript">
	function getURLParameter(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
	}

	window.onload = function() {
		var links = document.getElementsByTagName("a");
		for(var i=0;i<links.length;i++) {
			if(links[i].onclick&&links[i].onclick.toString().indexOf("getURL()") >= 0) {
				links[i].href = getURL();
				links[i].onclick = function() {
					location = getURL();
				}
			}
		}
	}

	function getURL() {
		var newurl = "https://newsite.site.com/";
		if (window.location.hostname == "oldsite.site.com") {
			newurl = "http://www.espn.com";
		} else {
			utm_source = getURLParameter('utm_source');
			utm_medium = getURLParameter('utm_medium');
			utm_campaign = getURLParameter('utm_campaign');
			utm_term = getURLParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://newsite.site.com/"+utm_source+"-"+utm_medium+"-"+utm_campaign+"-"+utm_term+"/start?utm_source="+utm_source+"&utm_medium="+utm_medium+"&utm_campaign="+utm_campaign+"&utm_term="+utm_term+"#newuser";
			}
		}
		return newurl;
		//alert("redirecting to "+newurl);
		//window.location = newurl;
	}	
</script>

Open in new window

Avatar of Gary
Gary
Flag of Ireland image

What is different instances?
What are getURLHelp, getURLAccount and getURLGift supposed to be getting?
An example of the url would help.
Avatar of MacLean Fitzgerald

ASKER

Apologies.  That was an obvious question.

GetURL does the following:

<script type="text/javascript">
	function getURLParameter(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
	}

	window.onload = function() {
		var links = document.getElementsByTagName("a");
		for(var i=0;i<links.length;i++) {
			if(links[i].onclick&&links[i].onclick.toString().indexOf("getURL()") >= 0) {
				links[i].href = getURL();
				links[i].onclick = function() {
					location = getURL();
				}
			}
		}
	}

	function getURL() {
		var newurl = "https://newsite.site.com/";
		if (window.location.hostname == "oldsite.site.com") {
			newurl = "http://www.espn.com";
		} else {
			utm_source = getURLParameter('utm_source');
			utm_medium = getURLParameter('utm_medium');
			utm_campaign = getURLParameter('utm_campaign');
			utm_term = getURLParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://newsite.site.com/"+utm_source+"-"+utm_medium+"-"+utm_campaign+"-"+utm_term+"/start?utm_source="+utm_source+"&utm_medium="+utm_medium+"&utm_campaign="+utm_campaign+"&utm_term="+utm_term+"#newuser";
			}
		}
		return newurl;
		//alert("redirecting to "+newurl);
		//window.location = newurl;
	}	
</script>

Open in new window


GetAccont does the following, in theory:

<script type="text/javascript">
	function getURLParameter(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
	}

	window.onload = function() {
		var links = document.getElementsByTagName("a");
		for(var i=0;i<links.length;i++) {
			if(links[i].onclick&&links[i].onclick.toString().indexOf("getURL()") >= 0) {
				links[i].href = getURL();
				links[i].onclick = function() {
					location = getURL();
				}
			}
		}
	}

	function getURL() {
		var newurl = "https://newsite.site.com#account";
		if (window.location.hostname == "oldsite.site.com") {
			newurl = "http://www.espn.com";
		} else {
			utm_source = getURLParameter('utm_source');
			utm_medium = getURLParameter('utm_medium');
			utm_campaign = getURLParameter('utm_campaign');
			utm_term = getURLParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://newsite.site.com/"+utm_source+"-"+utm_medium+"-"+utm_campaign+"-"+utm_term+"/start?utm_source="+utm_source+"&utm_medium="+utm_medium+"&utm_campaign="+utm_campaign+"&utm_term="+utm_term+"#account";
			}
		}
		return newurl;
		//alert("redirecting to "+newurl);
		//window.location = newurl;
	}	
</script>

Open in new window


Basically, it just changes #newuser to #account in 2 spots or #help or #gift.  There is probably a better way of doing this than what I have attempted, but I just don't know it.  Its starts to get a little complicated with different scenarios and this code works for the most part.  Just not when I try to duplicate the code and change getURL to getURLAccount.

My original code worked, except when someone wanted to click on a link and open in a new window, thus i asked for help and the above code was the outcome.   I would rather not post the real original code in an open forum, if you want I can email you separately.
You posted the same code twice,
Sort of, the only thing that changes is the piece where #newuser changes to #account.  Its different if you look a little closer.  So basically, in the content of the website I have this:

<a onclick="getURL()" href="#">link</a>

Open in new window


but I also have:

<a onclick="getURLAccount()" href="#">lin2</a>

Open in new window


I want each one to call a different script, even though the scripts are nearly identical except for the outputed URL.
Ok I get you have this bit different

...+utm_term+"#account";

So account, help and gift are just references for you to know which url to load?

And you are not using jQuery per chance???
I dont know JQuery
but its more complicated than that....since the scripts has other parts to it.
Well the simplified code is this (you don't need separate functions - just use the getURL function)

if(window.location.hash=="#account"){

}
else if(window.location.hash=="#help"){

}
else if(window.location.hash=="#gift"){

}

Open in new window

I don't know what you plan on doing depending on the hash tag.
Im not sure how that looks within the overall script?  Can I email you the original script?  It might make more sense that way.  here is my email:  mac@macleanw.com
Offsite communication is not allowed.
Just tell me what should be happening.
Is there a way to share something privately on this board?
It basically has to do what the original script does, which is above.  I just need to be able to call each function and get the desired URL.
I was off on the wrong tangent.

What should happen differently between getURL() and getURLAccount() - is it just the appending of the hashtag?
Correct.  Im trying to stick with the following script:

<script type="text/javascript">
	function getURLParameter(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
	}

	window.onload = function() {
		var links = document.getElementsByTagName("a");
		for(var i=0;i<links.length;i++) {
			if(links[i].onclick&&links[i].onclick.toString().indexOf("getURL()") >= 0) {
				links[i].href = getURL();
				links[i].onclick = function() {
					location = getURL();
				}
			}
		}
	}

	function getURL() {
		var newurl = "https://newsite.site.com/";
		if (window.location.hostname == "oldsite.site.com") {
			newurl = "http://www.espn.com";
		} else {
			utm_source = getURLParameter('utm_source');
			utm_medium = getURLParameter('utm_medium');
			utm_campaign = getURLParameter('utm_campaign');
			utm_term = getURLParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://newsite.site.com/"+utm_source+"-"+utm_medium+"-"+utm_campaign+"-"+utm_term+"/start?utm_source="+utm_source+"&utm_medium="+utm_medium+"&utm_campaign="+utm_campaign+"&utm_term="+utm_term+"#newuser";
			}
		}
		return newurl;
		//alert("redirecting to "+newurl);
		//window.location = newurl;
	}	
</script>

Open in new window


I just need to be able to use edit so that I can use the following when appropriate:

<a onclick="getURL()" href="#">link</a>
<a onclick="getURLAccount()" href="#">link</a>
<a onclick="getURLHelp()" href="#">link</a>
<a onclick="getURLGift()" href="#">link</a>

In the past I was able to just copy the original script 4x and just change the getURL as appropriate.  I do that now and it breaks it.
Change your links like so

<a onclick="getURL('Account')" href="#">link</a>

Change the start of the function like so

function getURL(val) {

You can then just amend the end of the url like so

...="+utm_term+"#"+val;
So for the account one:

<script type="text/javascript">
	function getURLParameter(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
	}

	window.onload = function() {
		var links = document.getElementsByTagName("a");
		for(var i=0;i<links.length;i++) {
			if(links[i].onclick&&links[i].onclick.toString().indexOf("getURL()") >= 0) {
				links[i].href = getURL();
				links[i].onclick = function() {
					location = getURL();
				}
			}
		}
	}

	function getURL(Account) {
		var newurl = "https://newsite.site.com/";
		if (window.location.hostname == "oldsite.site.com") {
			newurl = "http://www.espn.com";
		} else {
			utm_source = getURLParameter('utm_source');
			utm_medium = getURLParameter('utm_medium');
			utm_campaign = getURLParameter('utm_campaign');
			utm_term = getURLParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://newsite.site.com/"+utm_source+"-"+utm_medium+"-"+utm_campaign+"-"+utm_term+"/start?utm_source="+utm_source+"&utm_medium="+utm_medium+"&utm_campaign="+utm_campaign+"&utm_term="+utm_term+"#account";
			}
		}
		return newurl;
		//alert("redirecting to "+newurl);
		//window.location = newurl;
	}	
</script>

Open in new window


Thats it?
No, like this. You could have Account but it would become confusing.

function getURL(val) {


and at the end of newurl line change like so
utm_term+"#"+val;
I prob did it wrong, but it didnt work.  In the code, I only edit the function once right?

Just here:

function getURL(val) {
		var newurl = "https://newsite.site.com/";

Open in new window


Or is there another spot as well?
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial