简体   繁体   中英

Get Url, Modify Url, Open the new Url from image flag

Basically I have a website that is in HTML and some parts javascript!

The website is in english and portuguese: one version is inside a folder called "en" and portuguese is inside a folder "pt".

My idea is to have a flag for both and when I click on it... it will copy the active url, that is displayed in the browser, change "pt" to "en" or "en" to "pt", and then open again in the language that we choose! How can I do to make it working?!

Example: .../en/text.html ===> .../pt/text.html

I was trying...

<script language="javascript"> 
function getUrl(url)
      { var str = window.location.href;
        var url = str.replace("/en/","/pt/");
         alert(url);
     }</script>

But then what I could do to do it in html code?!

<a href="index.html" target="_self"><img src="../images/flag-pt.jpg"></a>

My question is: How can I get the Url, change it for a new one and then open in the same page?

Thank you a lot, All the best :)

You want to use window.location.replace instead of window.location.href .

More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM