Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,


Hopefully someone can assist with this question.

I am looking for simple JavaScript code that will update the a href link url and display it on a static HTML page, based on IP address from which the access request came.

The good thing is – the IP will be static and fixed, it will be the same number, which I already have.

In other words, if I access the same page from different IP addresses, the page will display different link url based on that.

While I might be wrong, my understanding is this can be scripted using ‘if else’ logic – if a specific IP address is detected and matched, the JS rewrites the link address.

Here is my logic below. Sorry, I don’t know much of the JavaScript syntax and used the construct below as a hypothetical example (its probably a total wreck since I didn't use the right syntax and whatnot), but this should give the general idea:



if (location.referrer.ip('123.45.67.89.00') > -1)

document.write.href = 'www.xxx.com';

else

document.write.href = 'www.yyy.com';


Again, this might not be the correct approach altogether and a way more sophisticated solution will be needed, therefore I hope that some of you with coding expertise can provide some assistance. Just to be clear - the script should NOT redirect the page but only update the url behind the link displayed on it instead.

I know there are several technologies already out there that can offer the solution, but my task is to get this running on the client side in the browser; I am not allowed to use server side technologies like Dot.NET, ASP, JSP, or anything other than JavaScript and static html.

Can someone please advice if this can be done this way and if yes, can you kindly provide some example so I can understand the logic? Basically, I just need to know if the approach I described above would work and if yes, what the syntax for 'if' and 'else' lines would be, in order for this to function properly.


Greatly appreciate any help, Thank YOU!

JK
Posted
Comments
Sergey Alexandrovich Kryukov 14-Jan-15 17:09pm    
It's easy to do, but you are doing something weird. Which URL do you want to change? It could be anchor's href, img's src, but not document.write (what is that?)...
—SA
Member 3879326 15-Jan-15 10:09am    
Sergey, thank you for reply,

I am indeed doing something weird, LoL – I’m not that good with JavaScript in the first place :)

The way I need this to work is first line of code with IF statement checks for IP address of whatever location the request call is coming from, but I don’t know what the syntax for the IP validation would be.
If IP address is validated successfully, it returns the specific output (2nd line), as a Link on the page. ELSE line should use the same output method except for a different url. So the idea is it serves different links on the page load based on whether one hits the page coming from a certain IP address, or not. The code syntax I provided is a mess but I hopped to at least illustrate the idea, maybe there is an entirely different approach to getting this to work, but I don’t know how. You mentioned it would be easy to do, can you please tell me how to do this? Many thanks!
Sergey Alexandrovich Kryukov 15-Jan-15 12:58pm    
No, you don't really illustrate the idea. Your www.com should be made some attribute of some element, or used in some request or redirection. What element, request? This is exactly you are asking about, "hot to go to different URL depending on IP", right? Then tell us what do you want to do with it. It's not very much of "approach". Say, "link of the page" may mean "anchor", something the user can click on. Or you want to redirect by script right away, without any click. So tell us what's the problem. And IP you can only determine on the server side...
—SA
Gideon van Dyk 14-Jan-15 18:52pm    
One thing will be certain, unless you are hosting this on a server that allows you to use Server Side Includes (SSI), you will not be able to get the client IP's with JavaScript alone.
Sergey Alexandrovich Kryukov 15-Jan-15 12:47pm    
Good point, not Javascript along...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900