Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JS: getURL() and href

Posted on 2014-03-06
19
Medium Priority
?
229 Views
Last Modified: 2014-04-16
I recently asked a question and received a great solution.  

See here for reference:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28369086.html#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

0
Comment
Question by:a204801
  • 10
  • 9
19 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39910345
What is different instances?
What are getURLHelp, getURLAccount and getURLGift supposed to be getting?
An example of the url would help.
0
 

Author Comment

by:a204801
ID: 39910542
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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39910567
You posted the same code twice,
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:a204801
ID: 39910583
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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39910656
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???
0
 

Author Comment

by:a204801
ID: 39910667
I dont know JQuery
0
 

Author Comment

by:a204801
ID: 39910677
but its more complicated than that....since the scripts has other parts to it.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39910694
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.
0
 

Author Comment

by:a204801
ID: 39910699
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
0
 
LVL 58

Expert Comment

by:Gary
ID: 39910721
Offsite communication is not allowed.
Just tell me what should be happening.
0
 

Author Comment

by:a204801
ID: 39910733
Is there a way to share something privately on this board?
0
 

Author Comment

by:a204801
ID: 39910746
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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39910792
I was off on the wrong tangent.

What should happen differently between getURL() and getURLAccount() - is it just the appending of the hashtag?
0
 

Author Comment

by:a204801
ID: 39910814
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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39910938
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;
0
 

Author Comment

by:a204801
ID: 39911045
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?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39911063
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;
0
 

Author Comment

by:a204801
ID: 39911282
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?
0
 
LVL 58

Accepted Solution

by:
Gary earned 1000 total points
ID: 39913272
That's it and at the end of the var newurl line should be like this

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+"#"+val;

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question