MacLean Fitzgerald
asked on
JS: getURL() and href
I recently asked a question and received a great solution.
See here for reference:
https://www.experts-exchange.com/questions/28369086/JS-getURL-and-href.html?anchorAnswerId=39910149#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:
See here for reference:
https://www.experts-exchange.com/questions/28369086/JS-getURL-and-href.html?anchorAnswerId=39910149#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>
ASKER
Apologies. That was an obvious question.
GetURL does the following:
GetAccont does the following, in theory:
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.
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>
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>
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.
You posted the same code twice,
ASKER
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:
but I also have:
I want each one to call a different script, even though the scripts are nearly identical except for the outputed URL.
<a onclick="getURL()" href="#">link</a>
but I also have:
<a onclick="getURLAccount()" href="#">lin2</a>
I want each one to call a different script, even though the scripts are nearly identical except for the outputed URL.
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???
...+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???
ASKER
I dont know JQuery
ASKER
but its more complicated than that....since the scripts has other parts to it.
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"){
}
I don't know what you plan on doing depending on the hash tag.
ASKER
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
Offsite communication is not allowed.
Just tell me what should be happening.
Just tell me what should be happening.
ASKER
Is there a way to share something privately on this board?
ASKER
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.
I was off on the wrong tangent.
What should happen differently between getURL() and getURLAccount() - is it just the appending of the hashtag?
What should happen differently between getURL() and getURLAccount() - is it just the appending of the hashtag?
ASKER
Correct. Im trying to stick with the following script:
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.
<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>
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.
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;
<a onclick="getURL('Account')
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;
ASKER
So for the account one:
Thats it?
<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>
Thats it?
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;
function getURL(val) {
and at the end of newurl line change like so
utm_term+"#"+val;
ASKER
I prob did it wrong, but it didnt work. In the code, I only edit the function once right?
Just here:
Or is there another spot as well?
Just here:
function getURL(val) {
var newurl = "https://newsite.site.com/";
Or is there another spot as well?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What are getURLHelp, getURLAccount and getURLGift supposed to be getting?
An example of the url would help.