Improve company productivity with a Business Account.Sign Up

x
?
Solved

JS: getURL() and href

Posted on 2014-02-19
12
Medium Priority
?
324 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 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39871527
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
ID: 39871535
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 83

Expert Comment

by:leakim971
ID: 39871564
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
LVL 2

Expert Comment

by:DualCool
ID: 39871601
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
ID: 39871620
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 83

Expert Comment

by:leakim971
ID: 39871642
with my test code, if you put your mouse over the link what do you see in the status bar ?
0
 
LVL 2

Expert Comment

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

Expert Comment

by:Gary
ID: 39871646
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
ID: 39871730
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
ID: 39871902
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
ID: 39910119
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
ID: 39910149
You should ask a new question and reference this.
0

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

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
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

579 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