Home > Article > Web Front-end > JS method to randomly generate hyperlink addresses on web pages_javascript skills
The example in this article describes how to use JS to randomly generate hyperlink addresses on web pages. Share it with everyone for your reference, the details are as follows:
This is a JavaScript application. Every time the page is refreshed, the link will be automatically changed. Although it is not commonly used, the use of the Javascript random function Math.random() will have a guiding effect.
The screenshot of the running effect is as follows:
The online demo address is as follows:
http://demo.jb51.net/js/2015/js-math-rand-url-show-codes/
The specific code is as follows:
<html> <head> <title>在网页上随机产生超链接地址</title> <script language="javascript"> <!-- url = new Array(); url[0] = "http://www.yahoo.com"; url[1] = "http://www.jb51.net"; url[2] = "http://www.baidu.com"; i = Math.random() * url.length; i = Math.floor(i); str = "<a id='linkID' href='" + url[i] + "'>"; --> </script> </head> <body> <script language="javascript"> <!-- document.write(str + "本链接是随机产生的,刷新页面会更换。</a><br>"); document.write(linkID.href); //--> </script> </body> </html>
I hope this article will be helpful to everyone in JavaScript programming.