html调用js变量和函数的几个方法

<span style="font-size:18px;"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  <script type="text/javascript"> 
var RestID = 2;//javascript变量
function a() {//javascript函数

    window.open("showNews.aspx?id= " + RestID);
}

</script> 
</head>

<body>
    <form id="form1" runat="server">
    <script language="javascript" type="text/javascript">
    var str1,str2
    str1="fdsgdg dsfdsf china"
    str2="武汉市广播电视大学"
    document.write("显示字符串1:"+str1+"<br>")
    document.write("显示字符串2:" + str2 + "<br>")

  //document.write()函数的写法很重要,写好才能正确的读到javascript的参数
<span style="color:#ff6666;">//方式一,打开新窗口,转到新链接

</span>    document.write(" <a   href= 'showNews.aspx?id="+RestID+"' style='text-decoration:none; color:Black;' target='_blank'> 目标页 </a> ")
   </script>

<span style="color:#ff9966;">//方式二,使用javascript:location.href,但是不能在新窗口中打开链接
</span>    <a   href= "javascript:location.href= 'showNews.aspx?id= ' + RestID " style=" text-decoration:none; color:Black;"  target="_blank"> 目标页 </a>

<span style="color:#33cc00;">//方式三,使用javascript:open,只能在搜狗浏览器里打开,在IE、火狐、360浏览器里面都打不开,就是说明这个方法不太好

</span>    <a   href= "javascript:open( 'showNews.aspx?id= ' + RestID)" style=" text-decoration:none; color:Black;"  target="_blank"> 目标页 </a>

<span style="color:#ff6666;">//方式四使用window.open但是鼠标为箭头而不是表示链接时的手型,

</span>    <a  οnclick= "window.open( 'showNews.aspx?id= ' + RestID)" style=" text-decoration:none; color:Black;"  target="_blank"> 目标页 </a>
<span style="color:#009900;">//方式五调用 javascript函数   ,但是鼠标为箭头而不是表示链接时的手型,
</span>
<a  οnclick= "javascript:a()" style=" text-decoration:none; color:Black;"  target="_blank"> 目标页 </a></form>

</body>
</span>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在父页面中调用子页面的方法变量,可以使用以下几种方式: 1. 使用iframe.contentWindow属性获取子页面的window对象,然后调用该对象的方法或访问该对象的属性。 例如,假设iframe的id为"myFrame",子页面中有一个名为"myFunction"的方法和一个名为"myVariable"的变量,可以使用以下代码来调用它们: ```javascript var iframe = document.getElementById("myFrame"); var childWindow = iframe.contentWindow; childWindow.myFunction(); var myValue = childWindow.myVariable; ``` 2. 在子页面中定义一个全局变量函数,并在父页面中通过iframe对象的contentWindow属性访问它们。 例如,子页面中定义了一个名为"myFunction"的全局函数和一个名为"myVariable"的全局变量,可以使用以下代码在父页面中访问它们: ```javascript var iframe = document.getElementById("myFrame"); var childWindow = iframe.contentWindow; childWindow.myFunction(); var myValue = childWindow.myVariable; ``` 3. 在父页面中使用postMessage API向子页面发送消息,并在子页面中监听该消息,并根据消息内容执行相应的操作。 例如,在父页面中发送消息: ```javascript var iframe = document.getElementById("myFrame"); var childWindow = iframe.contentWindow; childWindow.postMessage("hello", "http://example.com"); ``` 在子页面中监听消息: ```javascript window.addEventListener("message", function(event) { if (event.origin == "http://example.com" && event.data == "hello") { // 执行相应的操作 } }); ``` 需要注意的是,在使用postMessage API时需要注意安全性,防止被恶意代码利用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值