Solved

JS: getURL() and href

Posted on 2014-02-19
12
293 Views
Last Modified: 2014-03-06
I have the following JS:

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

		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";
				}
			}
			//alert("redirecting to "+newurl);
			window.location = newurl;
		}	
		</script>

Open in new window


With the following href calling the function:

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

Open in new window


This works perfectly fine when someone clicks on the link, but if they try and open the link in a new window it can no longer call the function.  It just opens the same page in a new window since its taking the # sign from the href.

any suggestions on a way to combat this?  Make the JS getURL pring the correct url whenn the page renders?
0
Comment
Question by:a204801
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Try this :

<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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Change it to a span instead of a link then there is no link to open or remove the href="#" and style the anchor to look like a link
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 
LVL 2

Expert Comment

by:DualCool
Comment Utility
Cathal's right - use a button or a span instead of a link. This will get rid of the right click menu with the open in new window option.

so instead of
<a onclick="getURL()" href="#">Link</a>

something like
<span onClick="getURL()" style="color:blue; cursor:pointer;">LINK</span>
0
 

Author Comment

by:a204801
Comment Utility
That's not helpful in the sense that I don't mind if people open in a new window.  I just want it to be correct URL.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
with my test code, if you put your mouse over the link what do you see in the status bar ?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:DualCool
Comment Utility
then leakim971's solution might be the way to go
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Then use leakims code tho the logic here seems misplaced since you have the values at your server so why not just create a normal link with the values - what is the point of all the javascript when you have nothing dynamic.
0
 

Author Comment

by:a204801
Comment Utility
my hosting provider doesnt allow me to do this in PHP,  so I had to use JS.

The original code was this:

<?php
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL;
if ($host == 'old.site.com')
{
    $newURL = 'http://espn.com';
}
elseif 
(  isset($_GET['utm_medium']) 
&& isset($_GET['utm_source']) 
&& isset($_GET['utm_campaign']) 
&& isset($_GET['utm_term']) 
) 
{
    $newURL = sprintf('https://new.site.com/%s-%s-%s-%s/start?utm_source=psc&utm_medium=website&utm_term=subpage&utm_campaign=linkage'
    , strip_tags($_GET['utm_medium'])
    , strip_tags($_GET['utm_source'])
    , strip_tags($_GET['utm_campaign'])
    , strip_tags($_GET['utm_term']) 
    );
} 
else 
{
    $newURL = 'https://new.site.com/start?utm_source=psc&utm_medium=website&utm_term=subpage&utm_campaign=linkage';
}

?>

Open in new window


leakims code worked as expected and I will implement.  

Thanks everyone!  So helpful!
0
 

Author Comment

by:a204801
Comment Utility
One final questionfor leakims.

I have for different pages I want to serve up.  So I might use any one of the following:

getURL()
getURLAccount()
getURLHelp()
getURLGifts()

Previously, I was just copying each one.  Changing the the getURLxx () for each one.

This doesnt seem to work with your code.  

The only difference between all the versions is

	function getURLAccount() {
		var newurl = "https://new.site.com/fff-free/start#account";

Open in new window


and
else {
			utm_source = getURLAccountParameter('utm_source');
			utm_medium = getURLAccountParameter('utm_medium');
			utm_campaign = getURLAccountParameter('utm_campaign');
			utm_term = getURLAccountParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://new.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";
			}

Open in new window


Its then repeated for HELP and GIFTS.

There might be a way to condense all 4 scripts into 1. Assuming I stick with 4 different scripts, the code seems to not work when I add more than 1.  This is how it looks in totality now:

<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://new.site.com/fff-free/start#newuser";

        if (window.location.hostname == "old.site.com") {
			newurl = "http://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://new.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>

<script type="text/javascript">
		function getURLAccountParameter(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("getURLAccount()") >= 0) {
				links[i].href = getURLAccount();
				links[i].onclick = function() {
					location = getURLAccount();
				}
			}
		}
	}
	function getURLAccount() {
		var newurl = "https://new.site.com/fff-free/start#account";

        if (window.location.hostname == "old.site.com") {
			newurl = "http://espn.com";
		} else {
			utm_source = getURLAccountParameter('utm_source');
			utm_medium = getURLAccountParameter('utm_medium');
			utm_campaign = getURLAccountParameter('utm_campaign');
			utm_term = getURLAccountParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://new.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> 

<script type="text/javascript">
		function getURLHelpParameter(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("getURLHelp()") >= 0) {
				links[i].href = getURLHelp();
				links[i].onclick = function() {
					location = getURLHelp();
				}
			}
		}
	}
	function getURLHelp() {
		var newurl = "https://new.site.com/fff-free/start#help";

        if (window.location.hostname == "old.site.com") {
			newurl = "http://espn.com";
		} else {
			utm_source = getURLHelpParameter('utm_source');
			utm_medium = getURLHelpParameter('utm_medium');
			utm_campaign = getURLHelpParameter('utm_campaign');
			utm_term = getURLHelpParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://new.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+"#help";
			}
		}
		return newurl;
		//alert("redirecting to "+newurl);
		//window.location = newurl;
	}	

</script> 

<script type="text/javascript">
		function getURLGiftsParameter(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("getURLGifts()") >= 0) {
				links[i].href = getURLGifts();
				links[i].onclick = function() {
					location = getURLGifts();
				}
			}
		}
	}
	function getURLGifts() {
		var newurl = "https://new.site.com/fff-free/start#gifts";

        if (window.location.hostname == "old.site.com") {
			newurl = "http://espn.com";
		} else {
			utm_source = getURLGiftsParameter('utm_source');
			utm_medium = getURLGiftsParameter('utm_medium');
			utm_campaign = getURLGiftsParameter('utm_campaign');
			utm_term = getURLGiftsParameter('utm_term');
			if (utm_source && utm_medium && utm_campaign && utm_term) {
				newurl = "http://new.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+"#gifts";
			}
		}
		return newurl;
		//alert("redirecting to "+newurl);
		//window.location = newurl;
	}	

</script> 

Open in new window


Clearly, there is a conflict going on.  I just can't quite figure out what to change?
0
 

Author Comment

by:a204801
Comment Utility
Can anyone help me with my last update?  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 use 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.  Im 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

Thanks!
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
You should ask a new question and reference this.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now