JavaScript中window.location.href,window.location.replace和window.location.assign之间的区别?
窗口对象包括JavaScript中的location对象。它包括以下属性-
window.location.href
它返回当前页面的URL。
示例
<!DOCTYPE html> <html> <body> <p>Click below to get the complete URL of the page.</p> <button onclick = "display()">URL</button> <script> function display() { var res = location.href; document.write(res); } </script> </body> </html>
window.location.replace
用于替换当前文档。
示例
<!DOCTYPE html> <html> <body> <button onclick = "display()">Replace current document</button> <script> function display() { location.replace("https://www.qries.com") } </script> </body> </html>
window.location.assign
如果要加载新文档,请使用JavaScriptAssign。
示例
<!DOCTYPE html> <html> <body> <button onclick = "display()">Open new document</button> <script> function display() { location.assign("https://www.qries.com") } </script> </body> </html>