Javascript 将url传递给document.write
我一直在尝试将URL传递给此函数,但它似乎没有为我单击。我还在学习请问有谁能帮忙吗?我有很多限制,我正在努力解决的情况下,你想知道我在这里试图做什么 我希望在html中的href标记中包含URL test.html,而不是将其包含在函数本身中。这样我就可以在我所有的链接中重用这个函数。我希望这是有道理的 Javascript:Javascript 将url传递给document.write,javascript,html,Javascript,Html,我一直在尝试将URL传递给此函数,但它似乎没有为我单击。我还在学习请问有谁能帮忙吗?我有很多限制,我正在努力解决的情况下,你想知道我在这里试图做什么 我希望在html中的href标记中包含URL test.html,而不是将其包含在函数本身中。这样我就可以在我所有的链接中重用这个函数。我希望这是有道理的 Javascript: function myFunctionA() { var myWindow = window.open("", "MsgWindow", "width=1000,
function myFunctionA() {
var myWindow = window.open("", "MsgWindow", "width=1000,height=700");
myWindow.document.write('<iframe name="contentwin" src="test.html" scrolling="yes" frameborder="0" height="100%" width="100%"></iframe>');
myWindow.document.close();
}
Html:
使用ES5语法和标准字符串连接:
function myFunctionWithParam(pageName) {
console.log("Open " + pageName + "page");
}
myFunctionWithParam("test.html");
使用ES6语法、箭头函数和模板文本的相同示例:
const myFunctionWithParam = (pageName) => {
console.log(`Open ${pageName} page`);
}
myFunctionWithParam("test.html");
尝试在代码示例中实现它。祝你好运 ES6
可以使用勾号将数据插入字符串,然后使用${my_variable}在字符串中使用变量。这些被称为
它看起来像这样:
函数MyFunctionURL{
var myWindow=window.open/myPage.html,MsgWindow,宽度=1000,高度=700;
myWindow.document.write`;
myWindow.document.close;
}
myFunctionA'http://example.com'
myFunctionA'http://another-example.com'
ES5
您可以使用+运算符来编辑
它看起来像这样:
函数MyFunctionURL{
var myWindow=window.open/myPage.html,MsgWindow,宽度=1000,高度=700;
myWindow.document.write;
myWindow.document.close;
}
myFunctionA'http://example.com'
myFunctionA'http://another-example.com'
海螺
使用concat方法可以传递无限的参数,并且它们都将连接在一起。这里我传递一个数组,但使用
函数MyFunctionURL{
var myWindow=window.open/myPage.html,MsgWindow,宽度=1000,高度=700;
可变项目=[
];
myWindow.document.write.concat…项;
myWindow.document.close;
}
myFunctionA'http://example.com'
myFunctionA'http://another-example.com'
由于您是JavaScript新手,最好不要从一开始就养成坏习惯。迄今为止最糟糕的做法之一是使用内联HTML事件属性onclick、onmouseover等。。有很多理由不使用它们,你可以看到它们 接下来,当您实际上不在任何地方导航时,不要使用a元素毕竟,您正在将href属性设置为表示您实际上不想去任何地方。它在语义上是不正确的,并且可能会给依赖屏幕阅读器访问您的页面的人带来问题。几乎任何元素都可以有一个click事件,因此div或button元素就可以做到这一点 或者,如果您不想自己创建和打开新窗口,那么a是合适的,您只需要:
<a href="test.html" target="_blank">link</a>
link您可以创建一个看起来像普通HTML链接的链接,但也可以调用一个函数。将this传递给函数,以获取对进行函数调用的原始对象的引用。在本例中,a href
<a href="test.html" onClick="myFunctionA(this); return false">test page</a>
当然,您可以使用一些聪明的模板将hrefTarget包含到字符串中,但我使用简单的老式+字符串,只是为了继续讨论这个主题
工作示例如下:
<body>
<div>
<ul>
<li><a href="test.html" onClick="myFunctionA(this); return false">test page</a>
<li><a href="otherpage.html" onClick="myFunctionA(this); return false">other page</a>
</ul>
</div>
<script>
function myFunctionA(myobject) {
var myWindow = window.open("", "MsgWindow", "width=1000,height=700");
var hrefTarget = myobject.href;
myWindow.document.write('<iframe name="contentwin" src=' + hrefTarget + ' scrolling="yes" frameborder="0" height="100%" width="100%"></iframe>');
myWindow.document.close();
}
</script>
</body>
传递什么url??您是否在寻求让MyFunction打开不同url的方法,具体取决于它的使用方式?查看函数参数和字符串连接。是的,我想使用此函数打开多个链接。例如,与设置iframe src=test.html不同,我希望它是iframe src=URL,然后通过test.html作为链接的URL。现在有什么意义了吗?不应该使用onclick的内联HTML事件属性。100年前我们就是这样做的。请参阅不使用它们的许多原因。非常感谢@Scott Marcus。我实际上是在向新窗口添加其他信息。我在新窗口的顶部添加了一些图像,作为顶部导航。我之所以包含iframe,是因为我希望在单击这些顶部导航按钮时可以填充相同的iframe。出于这个原因,我必须使用iframes,对吗?@Irma你不必,不。今天,我们进行AJAX调用,以异步方式转到服务器,获取需要注入页面的新内容。不过,我会更新我的答案以添加iframe。你仍然会使用我描述的相同技术。
var hrefTarget = myobject.href;
<body>
<div>
<ul>
<li><a href="test.html" onClick="myFunctionA(this); return false">test page</a>
<li><a href="otherpage.html" onClick="myFunctionA(this); return false">other page</a>
</ul>
</div>
<script>
function myFunctionA(myobject) {
var myWindow = window.open("", "MsgWindow", "width=1000,height=700");
var hrefTarget = myobject.href;
myWindow.document.write('<iframe name="contentwin" src=' + hrefTarget + ' scrolling="yes" frameborder="0" height="100%" width="100%"></iframe>');
myWindow.document.close();
}
</script>
</body>