javascript document.write("<base href=""&g

javascript document.write("<base href="">"); not working

本文关键字:quot href base lt write javascript document      更新时间:2023-09-26

以下代码工作:

<base href="http://www.w3schools.com/" target="_blank">    

但这不起作用:

<script>document.write('<base href="' + document.location + '" />');</script>
<script type="text/javascript">    
document.write("<base href='http://" + document.location.host + "' />");    
</script>    
<script type="text/javascript">    
document.write("<base href="http://www.w3schools.com/" target="_blank">");    
</script>    

http://www.customtemplates.com.au/

只需转义您的引号:

document.write("<base href='"http://www.w3schools.com/'" target='"_blank'">");    

或者用简单的引号更简单:

document.write('<base href="http://www.w3schools.com/" target="_blank">');    

你正在嵌套双引号。尝试切换到单引号。

<script type="text/javascript">    
document.write('<base href="http://www.w3schools.com/" target="_blank">');    
</script>    

请不要使用 document.write。请改用 createElement。

var head = document.getElementsByTagName('head')[0]; //get the head-Element
var script = document.createElement('script'); //Create a script-element
script.setAttribute('type', 'text/javascript'); //Set an attribute
var base = document.createElement('base'); //Create a base-element
base.setAttribute('href', document.location); //Set an attribute
script.appendChild(base); //Append the base-element to the script-element
head.appendChild(script);  //Append the script-element to the head-element