PHP 中的 JavaScript 不起作用(document.getElementById)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13262402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:15:22  来源:igfitidea点击:

JavaScript inside PHP not working (document.getElementById)

phpjavascriptiframedocumentgetelementbyid

提问by Albert Renshaw

Something weird is happening but I can't seem to get the JavaScript code document.getElementByIdworking inside of PHP...

发生了一些奇怪的事情,但我似乎无法让 JavaScript 代码document.getElementById在 PHP 中工作......

For example, load the following PHP code (below) into a PHP file and run it, there is no JavaScript alert? But if you copy the source-code that the PHP echoed (or printed) and run it as an HTML file there is a JavaScript alert? So any element that is created inside of PHP tags doesn't run in JavaScript, even if the JavaScript is kept outside of the PHP tags?

比如将下面的PHP代码(如下)加载到一个PHP文件中并运行,没有JavaScript警报?但是,如果您复制 PHP 回显(或打印)的源代码并将其作为 HTML 文件运行,是否会出现 JavaScript 警报?那么任何在 PHP 标签内创建的元素都不会在 JavaScript 中运行,即使 JavaScript 被保留在 PHP 标签之外?

Here is the PHP demo code:

这是 PHP 演示代码:

<?php 
print "
<iframe id='my_id' name='my_id' src='http://www.php.com/'></iframe>

<SCRIPT LANGUAGE='javascript'> 
document.getElementById('my_id').contentWindow.onload = function(){
    alert('content loaded');
}
</SCRIPT>
";
?>

It even doesn't work if just this is your code:

如果这只是您的代码,它甚至不起作用:

<iframe id='my_id' name='my_id' src='<?php echo"http://www.php.com/"; ?>'></iframe>

<SCRIPT LANGUAGE='javascript'> 
document.getElementById('my_id').contentWindow.onload = function(){
    alert('content loaded');
}
</SCRIPT>

Here is the source code that appears (upon request) (Also the contentWindow.onLoadis working fine for content that is not on the same domain as mine in Safari):

这是出现的源代码(根据要求)(contentWindow.onLoad对于与 Safari 中的域不在同一域中的内容,该代码也可以正常工作):

<iframe id='my_id' name='my_id' src='http://www.php.com/'></iframe>

<SCRIPT LANGUAGE='javascript'> 
document.getElementById('my_id').contentWindow.onload = function(){
    alert('content loaded');
}
</SCRIPT>

My issue is that in HTML this code works fine and the alert is called.... in PHP the code does not work and the alert is never called... There is something wrong with the way PHP handles document.getElementById, there is nothing wrong with .contentWindow.onload.

我的问题是,在HTML的代码工作正常,警报被称为....在PHP代码不工作和警报从来没有所谓的......也有一些是错误的方式PHP处理document.getElementById,有什么错.contentWindow.onload.

回答by Ruben-J

<iframe id='my_id' onload="alert('Content Loaded');" name='my_id' src='http://www.php.com/'></iframe>

Or better

或更好

<iframe id='my_id' onload='ShowAlert();' name='my_id' src='http://www.php.com/'></iframe>   

<script type='text/javascript'>
function ShowAlert(){
    alert('Content Loaded');
}
</script>

Or if you want to echo it

或者如果你想回应它

<?php
echo "<iframe id='my_id' onload='ShowAlert();' name='my_id' src='http://www.php.com/'></iframe>   

<script type='text/javascript'>
function ShowAlert(){
    alert('Content Loaded');
}
</script>"; ?>

回答by Manish

<iframe id='id' onload='display();' name='my_id' src='www.test.com'></iframe>   

<script type='text/javascript'>
function display(){
    alert('Loading');
}
</script>

回答by mcfedr

It fails because you call document.getElementByIdbefore the document is loaded, if you want to attached the frames onloadhandler like this you need to put the call to document.getElementByIdwithin the body onload.

它失败是因为您document.getElementById在加载文档之前调用,如果您想像这样附加框架onload处理程序,您需要将调用document.getElementById放在 body 中onload

Something like:

就像是:

<script>
    function loaded() {
        document.getElementById('my_id').contentWindow.onload = function(){
            alert('content loaded');
        }
    }
</script>
<body onload="loaded()">
<iframe id='my_id' name='my_id' src='http://www.php.com/'></iframe>
</body>

Even better grap a framwork like jQueryand use the domreadyevent. Its pretty standard to wrap all javascript code in a domreadyhandler.

甚至更好地抓住像jQuery这样的框架并使用该domready事件。将所有 javascript 代码包装在domready处理程序中是非常标准的。

There is the alternative mentioned by others that you directly attach the frames onload handler by putting it in the html.

其他人提到了另一种选择,您可以通过将其放入 html 来直接附加帧加载处理程序。

PHP isn't having any effect here.

PHP 在这里没有任何影响。

回答by Pheonix

Attach the function to the onLoad of the DOM itself and not to the contentWindow of the DOM element.

将函数附加到 DOM 本身的 onLoad 而不是 DOM 元素的 contentWindow。

<iframe id='my_id' name='my_id' src='http://www.php.com/'></iframe>

<script> 
    document.getElementById('my_id').onload = function(){
     alert('content loaded');
    }
</script>????????????????????????????????

Hence in PHP

因此在 PHP 中

<?php
echo <<< xyz
<iframe id='my_id' name='my_id' src='http://www.php.com/'></iframe>

    <script> 
        document.getElementById('my_id').onload = function(){
         alert('content loaded');
        }
    </script>
xyz;

?>

Which works for me.

这对我有用。

回答by janenz00

Your issue is not related to the php part of the code. Your code works fine when I try to load a file from the same domain, as shown below:

您的问题与代码的 php 部分无关。当我尝试从同一个域加载文件时,您的代码工作正常,如下所示:

   <?php
       print "<iframe id='my_id' name='my_id' src='test.php'></iframe>

       <SCRIPT LANGUAGE='javascript'> 

         document.getElementById('my_id').contentWindow.onload = function(){
             alert('content loaded');
         }
       </SCRIPT>
      ";
  ?>

The issue here is the ownership of the iframe.contentWindow when doing a cross domain request. This is one of the first linksthat shows up in Google when you search for iframe cross domains. See this portion:

这里的问题是进行跨域请求时 iframe.contentWindow 的所有权。当您跨域搜索 iframe 时,这是在 Google 中显示的第一个链接。看这部分:

The window object representing the iframe content is the property of the page that was loaded into the iframe. In order for the containing page to access the iframe's window object in any meaningful way, the domain of the containing page and the iframe page need to be the same (details).

表示 iframe 内容的 window 对象是加载到 iframe 中的页面的属性。为了让包含页面以任何有意义的方式访问 iframe 的 window 对象,包含页面和 iframe 页面的域需要相同(详细信息)。

That means, you can access the contentWindow only when you load content from the same domain.

这意味着,只有当您从同一域加载内容时才能访问 contentWindow。

Check the answer to this SO questiontoo.

也检查这个SO 问题的答案。

回答by Kotzilla

try this

试试这个

<?php

echo "<iframe id='my_id' name='my_id' src='http://www.php.com/'></iframe>

<script type='text/javascript'>
document.getElementById('my_id').onload = function(){     
  alert('content loaded'); 
}
</script>";

?>

回答by SuperKael

Ruben-J is right, use the onload within the html iframe, it is easier and as far as i know cannot be done with php, embeded javascript or not

Ruben-J 是对的,在 html iframe 中使用 onload,它更容易,据我所知不能用 php 完成,嵌入 javascript 与否

回答by xelber

Are you sure it works in an HTML file? have you tested it?

你确定它适用于 HTML 文件吗?你测试过吗?

document.getElementById('my_id').onload = function(){ alert('content loaded'); }

document.getElementById('my_id').onload = function(){ alert('content loaded'); }

try that

试试看