我有这个javascript函数,允许我使用此<script>Alert.render('message goes here');</script>
使用Javascript:
<script>
function CustomAlert(){
this.render = function(dialog){
var winW = window.innerWidth;
var winH = window.innerHeight;
var dialogoverlay = document.getElementById('dialogoverlay');
var dialogbox = document.getElementById('dialogbox');
dialogoverlay.style.display = "block";
dialogoverlay.style.height = winH+"px";
dialogbox.style.left = (winW/2) - (550 * .5)+"px";
dialogbox.style.top = "200px";
dialogbox.style.display = "block";
document.getElementById('dialogboxhead').innerHTML = "Image Upload Error!";
document.getElementById('dialogboxbody').innerHTML = dialog;
document.getElementById('dialogboxfoot').innerHTML = '<button id = "buttonclose" onclick="Alert.ok()">ok</button>';
}
this.ok = function(){
document.getElementById('dialogbox').style.display = "none";
document.getElementById('dialogoverlay').style.display = "none";
}
}
var Alert = new CustomAlert();
HTML:
<div id = "dialogoverlay"></div>
<div id = "dialogbox">
<div>
<div id = "dialogboxhead"></div>
<div id = "dialogboxbody"></div>
<div id = "dialogboxfoot"></div>
</div>
</div>
在html中,我尝试调用函数(Alert.render ...)并且工作。但是,当我尝试在html上面的php中调用它时:
echo "<script>Alert.render('message goes here');</script>";
根本不起作用。有什么想法吗?
这是PHP代码:
<?php
if(isset($_POST['btn_sample'])){
echo "<script>Alert.render('sample message');</script>";
}
我在html上添加了这个:
<form action = "" method = "post">
<input type = "submit" name = "btn_sample" value = "sample">
</form>
答案 0 :(得分:1)
检查,
php
脚本。html
的php不包含this
js脚本文件的链接。并在window.onload
之后调用Js函数。他们会阻止第一个
echo "<script>window.onload=function(){Alert.render('message goes here');}</script>";
答案 1 :(得分:0)
您的渲染功能依赖于已经存在的元素&#34; dialogoverlay&#34;,&#34; Dialogbox&#34;等等,如果您调用HTML上方的函数,他们就不会这样做。< / p>
您需要将Alert.render()调用绑定到DOMContentLoaded事件,或者只是将脚本放在身体的底部,这通常是当今的首选练习。