在Web浏览器中打开新代码?

时间:2018-04-30 18:07:26

标签: javascript html

有没有办法可以使用锚标记<a>或JavaScript代码打开新网站,而是使用网址作为输入,打开自定义代码输入?

2 个答案:

答案 0 :(得分:0)

从文本框中读取测试,如下所示: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_text_value2

然后打电话。 window.open(URL)

<!DOCTYPE html>
<html>
<body>

Name: <input type="text" id="myText" value="http://www.google.com">

<p>Click the button open window.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    window.open(document.getElementById("myText").value)
    //Bravo";
}
</script>

</body>
</html>

答案 1 :(得分:0)

将您的网址格式化为:

 <a href="javascript:alert('hello!');">Click here</a>

Hovever,这是一个JS注入和大多数网站,包括SO过滤掉它