• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

JS: getURL() and href

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
a204801
Asked:
a204801
  • 4
  • 3
  • 3
  • +1
1 Solution
 
leakim971PluritechnicianCommented:
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
 
GaryCommented:
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
 
leakim971PluritechnicianCommented:
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DualCoolCommented:
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
 
a204801Author Commented:
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
 
leakim971PluritechnicianCommented:
with my test code, if you put your mouse over the link what do you see in the status bar ?
0
 
DualCoolCommented:
then leakim971's solution might be the way to go
0
 
GaryCommented:
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
 
a204801Author Commented:
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
 
a204801Author Commented:
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
 
a204801Author Commented:
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
 
GaryCommented:
You should ask a new question and reference this.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now