javascript没有显示警报

时间:2012-08-15 17:46:33

标签: javascript

此代码未向我显示警告: -

<script type="text/javascript">
var wloc = window.parent.location;
var abcd = wloc.substring(0,24);
alert(wloc);
alert(abcd);
</script>

3 个答案:

答案 0 :(得分:3)

确保您的脚本可以访问parent。然后修改脚本,如下所示

<script type="text/javascript">
window.onload = function(){
    var wloc = window.parent.location.href;
    var abcd = wloc.substring(0,24);
    alert(wloc);
    alert(abcd);
}
</script>

对于当前窗口的位置,请尝试下面的脚本

<script type="text/javascript">
window.onload = function(){
    var wloc = window.location.href;
    var abcd = wloc.substring(0,24);
    alert(wloc);
    alert(abcd);
}
</script>

答案 1 :(得分:1)

.href之后添加window.parent.location

<script type="text/javascript">
window.onload = function(){
    var wloc = window.parent.location.href;
    var abcd = wloc.substring(0,24);
    alert(wloc);
    alert(abcd);
}
</script>

答案 2 :(得分:0)

下面的行是错误的。这就是它没有显示警报的原因。

var abcd = wloc.substring(0,24); 

改为使用

<script type="text/javascript">
  window.onload = function(){
    var wloc = window.parent.location.href;
    var abcd = wloc.substring(0,24);
    alert(wloc);
    alert(abcd);
 }
</script>