How can I add url to this script

Hello can a member tell me about how to link a url with this below code.This code is about to disable button after click for specific time kindly help me how can I add url to this code

<html>
    <head>  
    </head>
    <body>
        <button id="button">click me</button>
    </body>
    <script type="text/javascript">
        var myButton=document.getElementById("button");
        function storeTime(theTime)
        {
            localStorage.setItem("time", theTime);
        }
        
        function check()
        {
            var now=new Date().getTime();
            if(!localStorage.getItem("time"))
            {
                storeTime(now);
            }
            var storedTime=parseInt(localStorage.getItem("time"));
            
            if(storedTime>now)
            {
                myButton.disabled=true;
            }
            else
            {
                myButton.disabled=false;
            }
        }
        
        function buttonClicked()
        {
            var now=new Date().getTime() + 180000;
            storeTime(now);
            myButton.disabled=true;
        }
        
        setInterval(check, 1000);
        check();
        myButton.addEventListener("click", buttonClicked);
    </script>
</html>

Assuming you mean you want the user to move when they click the button, add window.location.href = <the URL there> to buttonClicked();

1 Like

Yeah I know that but kindly tell me where I set that code

…

Okay thanks…I will try

To link a URL with your code so that clicking the button takes the user to a new URL after it’s enabled, you can modify the buttonClicked function to include window.location.href .

If I add window.location.href then timer is not working kindly send me a complete code that works for me

<head>  
</head>
<body>
    <button id="button">Click Me</button>
</body>
<script type="text/javascript">
    var myButton = document.getElementById("button");
    function storeTime(theTime) {
        localStorage.setItem("time", theTime);
    }
    
    function check() {
        var now = new Date().getTime();
        if (!localStorage.getItem("time")) {
            storeTime(now);
        }
        var storedTime = parseInt(localStorage.getItem("time"));
        
        if (storedTime > now) {
            myButton.disabled = true;
        } else {
            myButton.disabled = false;
        }
    }
    
    function buttonClicked() {
        var now = new Date().getTime() + 180000; // disables the button for 3 minutes
        storeTime(now);
        myButton.disabled = true;
        window.location.href = 'https://www.example.com'; // Redirect immediately
    }
    
    setInterval(check, 1000);
    check();
    myButton.addEventListener("click", buttonClicked);
</script>
Let me know if its working for you?

Credits: copied from carxstreetapk.com

This code worked great but the problem is that this code is hackable with date…If we change today’s date to new date then the button become clickable.

We can modify the code to use the Date.now() method for comparing timestamps instead of relying on the client’s system time.

<html>
<head>  
</head>
<body>
    <button id="button">Click Me</button>
</body>
<script type="text/javascript">
    var myButton = document.getElementById("button");

    function storeTime(theTime) {
        localStorage.setItem("time", theTime);
    }

    function check() {
        var now = Date.now();
        var storedTime = parseInt(localStorage.getItem("time")) || 0;
        
        if (storedTime > now) {
            myButton.disabled = true;
        } else {
            myButton.disabled = false;
        }
    }

    function buttonClicked() {
        var now = Date.now() + 180000; // disables the button for 3 minutes
        storeTime(now);
        myButton.disabled = true;
        window.location.href = 'https://www.example.com'; // Redirect immediately
    }

    setInterval(check, 1000);
    check();
    myButton.addEventListener("click", buttonClicked);
</script>
</html>

(Date.now() is also the client’s system time.)

The simple answer is that you cant prevent someone from enabling a button you’ve put down; your server-side needs to handle the interaction if you want it to not be bypassable.

In Your last html code same issue I found…still button is hackable with change of date

Kindly help me…I need that html code through which I prevent others by clicking again and again and that is unhackable also…

As i said, Javascript is unable to prevent this. You will need to implement server-side logic.

I have one html code that is unhackable but I can’t set url in that code…Kindly help me…I shall be very thankful to you

What i mean is you cant stop someone from pushing the button. What happens when the button is pushed can be changed.

I dont know what server side language(s) your host is running. PHP? ASP? Python? Node?
Whatever language it is, the abstraction would be basically:

User requests page.
Server makes note of the user, starting a session for them; it notes the time and stores that internally.
Server sends page to the user.
User clicks the button.
Button sends a request to the server to get data (a URL, some data, whatever).
Server looks at the user’s session time, compares it to the server’s current time; if enough time has passed, send the appropriate data. If it hasnt, send an error.
The page’s javascript receives the answer from the server, and acts appropriately.

Click Me

Is this button clickable after modifying the code.