Zander Møysal
Zander Møysal

Reputation: 55

How to change url href in javascript based on page contents?

Here's my javascript part:

    function dateCheckThree() 
{   
    var d = new Date();
    var n = d.getDay()
    var lukeTre = document.getElementById("Luke3");
    if (n === 2)
    {
        lukeTre.href="home.html";
        confirm("Sorry, you gotta wait untill 3.Desember to enter this page.")  
    }
}

this is my html part:

    <a href="Luke3.html" id="Luke3" onClick="dateCheckThree()">
    <div-1c> 3 </div-1c>
    </a>

NOTE: The Javascript sentenece is true just for testing purposes. When i click the link, I get directed to Luke3.html

Upvotes: 1

Views: 130

Answers (2)

Naresh Walia
Naresh Walia

Reputation: 2022

Prevent the default behavior of anchor tag as :

 function dateCheckThree(e) 
{   

    var d = new Date();
    var n = d.getDay()
    var lukeTre = document.getElementById("Luke3");
    if (n === 2 )
    {
       e.preventDefault();
       alert("Sorry, you gotta wait untill 3.Desember to enter this page.")
       lukeTre.href="home.html";

    }
}

Upvotes: 2

vaso123
vaso123

Reputation: 12391

You need to confirm before redirect, and get the value if you want to confirm. But i think, you want to use alert not confirm.

var redirect = confirm("Sorry, you gotta wait untill 3.Desember to enter this page.");
if (redirect) {
    lukeTre.href = "home.html";
}

So I think, what you want something like this (overwrite the condition):

if (n === 2) {
    lukeTre.href = "home.html";
} else {
    alert("Sorry, you gotta wait untill 3.Desember to enter this page.");
}

Upvotes: 0

Related Questions