/    Sign up×
Community /Pin to ProfileBookmark

check current URL

Hi All,

Forgive me if this kind of script is already been discussed many times.
I’m not able to find the solution yet.

I’m trying to make a script that checks the current URL.
My website has different languages. If the visitor clicks the ‘language change link’ they must go to the ‘same’ page that is currently visible.

Like this:

if currentpage = welcome.php go to bonjour.php
if currentpage = contact.php go to contacter.php

Is this even possible?

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@PadonakApr 02.2012 — and what about the next page? you should save the user's choise and once he set his language every other page must be in this language.
Copy linkTweet thisAlerts:
@nick07authorApr 02.2012 — I want to connect the pages using javascript.

So javascript checks the current URL.

Like this:

If this currentpage = welcome.php then when link is clicked, goto bonjour.php

If this currentpage = contact.php then when link is clicked, goto contacter.php

ect
Copy linkTweet thisAlerts:
@PadonakApr 02.2012 — (cookie or session or D? + php wouldn't be better?
Copy linkTweet thisAlerts:
@nick07authorApr 02.2012 — Ofcourse that would be better.

But this is a simple webpage and I like to learn how to do this in javascript.

Is it a difficult script, just to check the url and then an if statement?
Copy linkTweet thisAlerts:
@thebigkrummApr 02.2012 — I think you could use window.location.href; it's like this:

[CODE]
var currentPage = window.location.href;
if(currentPage == 'http://whatever'){
... code here ...
}
[/CODE]
Copy linkTweet thisAlerts:
@PadonakApr 02.2012 — [CODE]<script type="text/javascript">
var pages=[
//every set is equal in length!!
['en','page1','page2','etc.'],
['fr','page1','page2','etc.'],
['de','page1','page2','etc.'],
['ru','page1','page2','etc.']
//etc.
];

//pass here lang like ru/fr/de/en etc.
function redir(lang){
var d=document.location.href,
page=d.substring(d.lastIndexOf('/')+1,d.lastIndexOf('.')),
ext=d.substring(d.lastIndexOf('.')+1,d.length),
pos_in_arr=-1;
for(var i in pages){if(pos_in_arr!=-1){break;}else{for(var j in pages[i]){if(page==pages[i][j]){pos_in_arr=j;break;}else{continue;}}}}
for(var n in pages){if(pages[n][0]!=lang){continue;}else{if(pos_in_arr!=-1){location.href=pages[n][pos_in_arr]+'.'+ext;}}}
}
//setTimeout('redir("fr");',2000);
</script>[/CODE]
Copy linkTweet thisAlerts:
@thebigkrummApr 02.2012 — yeah, or that ?
Copy linkTweet thisAlerts:
@PadonakApr 02.2012 — lol ))
Copy linkTweet thisAlerts:
@nick07authorApr 02.2012 — Thank you, I will test this.
Copy linkTweet thisAlerts:
@PadonakApr 02.2012 — you welcome, i've tested it locally
×

Success!

Help @nick07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...