如何实现不刷新页面,只重新加载js文件?

JavaScript08

如何实现不刷新页面,只重新加载js文件?,第1张

不刷新页面,重新加载js文件的方法有:

通过js创建script标签,例如

var script = document.createElement("script")

script.src = "test.js"

document.body.appendChild(script)

2.通过ajax实现无刷新加载,ajax的方法可以自己写,也可以引用,以下引用的是jquery的ajax

$.ajax({

    type = "get",

    url : "test.js",

    dataType : "script"

})

总结:如果是自己写js,不引用。建议用第一种,第二种也可以参考。

为是重新加载页面或刷新页面使用jquery,你可以使用location.reload()命令,

以下是代码:

<html>

<head>

<title>Refresh a page in jQuery</title>

<scripttype="text/javascript"src="jquery-1.3.2.min.js"></script>

</head>

<body>

<buttonid="PageRefresh">Refresh a Page in jQuery</button>

<scripttype="text/javascript">

$('#PageRefresh').click(function() {

location.reload()

})

</script>

</body>

</html>

一、Jquery动态加载Js和Css扩展方法

$.extend({

includePath: '',

include: function(file) {

var files = typeof file == "string" ? [file]:file

for (var i = 0i <files.lengthi++) {

var name = files[i].replace(/^\s|\s$/g, "")

var att = name.split('.')

var ext = att[att.length - 1].toLowerCase()

var isCSS = ext == "css"

var tag = isCSS ? "link" : "script"

var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' "

var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'"

if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">")

}

}

})

二、使用方法:

$.include('dedeajax.js')

$.include('android.css')

或者:

$.includePath='www/html/'

$.include([dedeajax.js','android.css'])