user3808370
user3808370

Reputation: 3

How to change a URL based on a condition

I need to change a URL of a link of an item based on my window.location condition. I am not really familiar with JAVA so any help or direction to this end would be appreciated. Thanks.

Here is the code I have used thus far to manually change the content of said item (its a workaround for a bilingual site made on a unilingual WP theme...)

<ul class="block-with-icons clearfix">
    <li class="b1">
        <a href="http://test.ca/?page_id=69">
            <h5><script>
                if(window.location.href== "http://testfrench.ca/?lang=fr"){
                    document.write("Bonjour")
                } else {       
                    document.write("Hello")
                }
            </script></h5>
            <span> <script>
                if(window.location.href== "http://testfrench.ca/?lang=fr"){
                    document.write("Informez-vous")
                } else {       
                    document.write("Get informed")
                }
            </script>
            </span>
        </a>
    </li>

What I need now is for the link to route to the french page and not just the english page upon the same style window.location condition.

Upvotes: 0

Views: 2381

Answers (3)

SubOne
SubOne

Reputation: 633

Add this to the end of your HTML:

<script>
    if (/[?&]lang=fr/.test(window.location.href)) {
        var anchors = document.getElementsByTagName('a');
        for (var i = 0; i < anchors.length; ++i) {
            var token = anchors[i].href.indexOf('?') == -1 ? '?' : '&';
            anchors[i].href = anchors[i].href.replace(
                /^(.*(?:[?&].*)?)(#.*)?$/,
                '$1' + token + 'lang=fr' + '$2'
            );
        }
    }
</script>

This will run through every anchor tag on the page an add lang=fr to the href.

The first conditional contains a regular expression:

/[?&]lang=fr/.test(window.location.href)

Which is a better way of saying "is the page in french" than:

window.location.href== "http://testfrench.ca/?lang=fr

...which forces you to have use this code on a page with that exact URL.

I recommend you store this in a boolean at the top of the page like so:

<script>
    var isFrench = /[?&]lang=fr/.test(window.location.href);
</script>

Then you can use it simply in your conditions:

if (isFrench) { /* ... */ }

getElementsByTagName gets all of the anchor tags on the page, and then we iterate each one, updating the href for each.

We test if there is a "?" in the URL already, in which case we want to use a "&" to attach the lang param instead.

Then there is another very simplified regular expression to get the first and last parts of the URL, and we replace the whole href with our lang param in the middle.

Upvotes: 0

tuan huynh
tuan huynh

Reputation: 727

try

try this. i think it work ok

<li class="b1">

    <script>
         if(window.location.href== "http://testfrench.ca/?lang=fr"){
             document.write("<a href=\"french page\">");
            document.write("<h5>Bonjour</h5>");
            document.write("<span>Informez-vous</span></a>")
         } else {       
             document.write("<a href=\"http://test.ca/?page_id=69\">");
             document.write("<h5>Hello</h5>");
             document.write("<span>Get informed</span></a>")
         }
    </script>
 </li>

Upvotes: 1

spasticparanoia
spasticparanoia

Reputation: 42

Should consider using javascript to directly get the QS such as this. That being said, what you're looking for is:

if(window.location.href== "http://testfrench.ca/?lang=fr"){
    window.location.href ='http://yoursiteurlforfrench/pageurl';
}

The window.location.href = will perform the page redirect for you, while the == in your if statement is doing a comparison for current page href to that string.

Upvotes: 0

Related Questions