I have built a large from and I have just discoverd my <button></buttom> does not work as a link

I am using

 <button onclick="window.location.href='myurl'">Next</button>

, but it is not working

Help.

Recommended Answers

All 10 Replies

Maybe I'm way off here but isn't the onclick event supposed to call a function? Maybe you have to include the link in a function and call it that way. Again, I may be way off but I'm just throwing my 2 pennies around right now.

You could be right, could you provide an example please

Sure, this should be close to what you need

<html>
    <head>
        <script type="text/javascript">
            function clicky(){
                location.href = 'http://www.daniweb.com';
            }
        </script>
    </head>
    <body>
        <button onclick="clicky();">Next</button>
    </body>
</html>

Cool, nice.

the only problem i have is that that there are 9 button on the main page that go to 9 different locations.

Do i do this 9 time?

thanks boss! :-)

My lack of experience with this issue says yes, create 9 functions calling them click1, click2,...,click9. However, there may be a better way to do that but I'm just not sure what it is. Maybe someone else will be able to jump in on this and let you know.

Thanks mate, this is really helpful.

No problem, hopefully this gets resolved quick for you.

You can still have one function called "clicky". Just pass the URL to the function as well...

<html>
    <head>
        <script type="text/javascript">
            function clicky(param){
                location.href = param;
            }
        </script>
    </head>
    <body>
        <button onclick="clicky('http://www.daniweb.com');">Next</button>
        <button onclick="clicky('http://www.other.com');">Another Button..</button>
    </body>
</html>
commented: Very cool, thanks for helping out and I learned something here too. +2
Member Avatar for Zagga

When you say your original code (<button onclick="window.location.href='myurl'">Next</button>)doesn't work, what exactly do you mean? I tried it in FireFox 22, Chrome 27 and Internet Explorer 10 and it worked fine.

yes zagga you are right. You can directly like directly like javascript in onclick event(if it is a single line ,without calling function.
I have been using <button onclick="window.location.href='myurl'">Next</button> since i was working on IE8,and still all browsers supports it.
For an example ,check this

<html>
<head><title>HREF</title></head>
<body>
<input type="button" onclick="window.location.href=('http://www.google.com')">
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.