ADVERTISEMENT
ADVERTISEMENT

JavaScript - Redirect to another URL on Button Click

In this code snippet we will learn How to redirect to another URL using JavaScript function, in this example we will take three buttons and redirect to other URLs through button click.

Redirect to another URL on Button Click

JavaScript function:

<script type="text/javascript">
   function redirectToURL(btnId){
   	if(btnId=="button1")
   		window.location.replace("https://en.wikipedia.org/wiki/Main_Page");
   	else if(btnId=="button2")
   		window.location.replace("https://www.google.com");
   	else if(btnId=="button3")
   		window.location.replace("http://www.bing.com/");
   }
</script>

HTML Source Code with JavaScript:

<!--JavaScript - Redirect to another URL on Button Click.-->
<html>
   <head>
      <title>JavaScript - Redirect to another URL on Button Click.</title>
      <script type="text/javascript">
         function redirectToURL(btnId){
         	if(btnId=="button1")
         		window.location.replace("https://en.wikipedia.org/wiki/Main_Page");
         	else if(btnId=="button2")
         		window.location.replace("https://www.google.com");
         	else if(btnId=="button3")
         		window.location.replace("http://www.bing.com/");
         }
      </script>
   </head>
   <body style="text-align: center">
      <h1>
      JavaScript - Redirect to another URL on Button Click.
      <h1>
      <input type="button" id="button1" value="www.wikipedia.com" onClick='redirectToURL(this.id)'/>
      <input type="button" id="button2" value="www.google.com" onClick='redirectToURL(this.id)'/>
      <input type="button" id="button3" value="www.bing.com" onClick='redirectToURL(this.id)'/>
   </body>
</html>

Result

redirect to another url using javascript function

JavaScript Examples »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.