executing javascript: link + refreshing. JS + PHP


executing javascript: link + refreshing. JS + PHP

好的,那么我该如何创建这个链接呢:

"javascript:elements.setPercentage('element1','".$value."')"

在我的页面上执行,当页面加载时?

你可以像这样把它放在body标签中:

<>之前<body onload="function(){elements.setPercentage('element1','".$value."')}"之前

或在你的javascript文件中:

<>之前window.onload = function(){elements.setPercentage('element1','".$value."')};之前

或jQuery

<>之前$(document).ready(function(){elements.setPercentage('element1','".$value."')});之前

你需要把它包装在一个函数中因为你要发送参数

本例将window.onload事件处理程序绑定到一个匿名函数。然而,在本例中,您只能有一个处理程序(但其中的代码可以随您的需要而多)。

例如,jQuery有一个更有效的方法onDOMReady,你应该考虑一下。
<head>
... other stuff
<script>
window.onload = function(){
    if (typeof elements == 'object' and typeof elements.setPercentage == 'function') {
        elements.setPercentage('element1','<?php echo $value; ?>');
    }
}
</script>
... other stuff
</head>

如果你使用的是原生JS:

<body onload="/*whatever*/">...</body>

如果你正在使用JQuery(更好的跨浏览器支持以下内容):

$(document).ready(function(){/*do whatever*/})