JavaScript 打开链接而无需点击
问题陈述: 如何使用JavaScript在不点击链接的情况下打开链接?
解决方案: 当鼠标移到文本上时,链接将被打开。它返回一个新创建的窗口,如果调用失败则返回NULL。
语法:
window.open( URL, name, Specs )
参数: 该函数接受如上所述的三个参数,并在下面进行描述:
- URL: 这是可选参数。它用于指定需要打开的网页的URL。如果未指定URL,则会打开一个新的窗口。
- Name: 这是一个可选参数,用于指定目标属性。
- _blank: URL加载到新窗口中。这是可选的。
- _top: URL替换当前页面。
- Specs: 这是一个可选参数。它是一个以逗号分隔的项目列表,没有空格。
- Height: 它表示窗口的高度(以像素为单位)。
- Width: 它表示窗口的宽度(以像素为单位)。
注意: 允许浏览器弹出窗口。
程序1: URL加载到新窗口中。
<!DOCTYPE html>
<html>
<head>
<title>Javascript open link without click</title>
<style>
.gfg {
text-align:center;
font-size:40px;
font-weight:bold;
color:green;
}
</style>
<script>
function myFunction() {
window.open("https://www.geeksforgeeks.org");
}
</script>
</head>
<body>
<div class = "gfg" onmouseover = "myFunction()">
GeeksforGeeks
</div>
</body>
</html>
输出:

程序2: 当前窗口中加载URL。
<!DOCTYPE html>
<html>
<head>
<title>Javascript open link without click</title>
<style>
.gfg {
text-align:center;
font-size:40px;
font-weight:bold;
color:green;
}
</style>
<script>
function myFunction() {
window.open("https://www.geeksforgeeks.org", "_top");
}
</script>
</head>
<body>
<div class = "gfg" onmouseover = "myFunction()">
GeeksforGeeks
</div>
</body>
</html>
输出:

程序3: 将URL加载到特定大小的新窗口中。
<!DOCTYPE html>
<html>
<head>
<title>Javascript open link without click</title>
<style>
.gfg {
text-align:center;
font-size:40px;
font-weight:bold;
color:green;
}
</style>
<script>
function myFunction() {
window.open('https://www.geeksforgeeks.org',
' ', 'width=500, height=300');
}
</script>
</head>
<body>
<div class = "gfg" onmouseover = "myFunction()">
GeeksforGeeks
</div>
</body>
</html>
输出:

极客教程