[英]What's a better way to call a JavaScript function in another file?
I'm doing a job of front-end at the time, and for this I am having to call various functions javascript / jQuery in other files. 我当时正在做前端工作,为此,我不得不在其他文件中调用各种函数javascript / jQuery。 So, I'm using PHP with CodeIgniter and I trying to make a call from a PHP function call another function javascrip.Follow what I'm trying to do: 因此,我将PHP与CodeIgniter结合使用,并且尝试从PHP函数进行调用以调用另一个函数javascrip。
PHP: PHP:
public function validaInclusao() {
getAtributoJanelaPai("slCusto");
}
function getAtributoJanelaPai($idCampo) {
carregaScriptsJquery();
echo "getAtributoJanelaPai('$idCampo');";
}
JavaScript: JavaScript:
function getAtributoJanelaPai(idCampo) {
alert('getAtributoJanelaPai');
var element = window.opener.document.getElementById(idCampo);
var value = '';
if (element.tagName === 'SELECT') {
value = element.selectedIndex + '|' + element.options[element.selectedIndex].value;
} else if (element.tagName === 'INPUT') {
value = element.value;
}
alert(value);
}
So, as it would be in line: 因此,就像下面这样:
echo "getAtributoJanelaPai('$idCampo');";
You can do that, but you have to import the Javascript file into your document. 您可以这样做,但是必须将Javascript文件导入到文档中。
In your head section put the following: 在您的头部插入以下内容:
<script type="text/javascript" src="myScripts.js"></script>
Where "myScripts.js" is the filename of the Javascript file. 其中“ myScripts.js”是Javascript文件的文件名。
And this is how you do the echo: 这就是您执行回显的方式:
<?php echo getAtributoJanelaPai($idCampo); ?>
Create javascript file that will return some thing 创建将返回某些内容的javascript文件
function getAtributoJanelaPai(idCampo) {
alert('getAtributoJanelaPai');
var element = window.opener.document.getElementById(idCampo);
var value = '';
if (element.tagName === 'SELECT') {
value = element.selectedIndex + '|' + element.options[element.selectedIndex].value;
} else if (element.tagName === 'INPUT') {
value = element.value;
}
return(value);
}
and in php file use this, include created js file first. 并在php文件中使用它,首先包含创建的js文件。
public function validaInclusao() {
getAtributoJanelaPai("slCusto");
}
function getAtributoJanelaPai($idCampo) {
carregaScriptsJquery();
echo "<script language=javascript>document.write(getAtributoJanelaPai(idCampo));</script>";
}
or try this one 或尝试这个
echo "<script language=javascript>getAtributoJanelaPai(idCampo);</script>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.