Solved

JS: getURL() and href

Posted on 2014-02-19
12
296 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
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 82

Expert Comment

by:leakim971
ID: 39871564
0
 
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 82

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 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

20 Experts available now in Live!

Get 1:1 Help Now