2014-10-09 18 views
1

我有一個輸入,它有一個動態值,因爲它是可編輯的。 Onkeypress在textarea或之後onclick點擊btn id時觸發事件,它應該在另一個iframe上添加輸入值,因爲輸入是可更改的,所以它是動態的。因此,如果沒有頁面加載,iframe的腳本innerHTML應該刷新。我做了一些事情,但沒有成功。我想動態追加腳本在onclick或onkeypress事件觸發後的iframe

HTML

<textarea id="inpt" cols="30" rows="10"></textarea> 
<div><input type="submit" value="append" id="btn" style="width:60px;height:20px;background:grey"></div><br> 
<iframe id="fr" name="fr" style="width:300px;height:300px;"></iframe> 

JS

  var _script = document.createElement('script'); 
      _script.type = 'text/javascript'; 
      var iFrame = document.getElementById('fr'), 
      iFrameBody, 
      t = document.getElementById("inpt"), 
      btn = document.getElementById('btn'); 

     function injectJS(content, val) { 
      if (iFrame.contentDocument) 
      { 
      iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0]; 
      } 
      else if (iFrame.contentWindow) 
      { 
      iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0]; 
      } 
      iFrameBody.appendChild(content); 
      content.innerHTML = val; 
      console.log(iFrameBody);  
     } 

     //t.onkeypress = injectJS(_script, t.value); 
     btn.onclick = injectJS(_script, t.value); 

而這裏的工作演示。 http://jsfiddle.net/n5fdywb9/

+0

參見另一篇文章(http://stackoverflow.com/questions/86428/whats-the-best-way-to-reload-an-iframe-using-javascript)我看到訪問框架的內容是這樣的:「document.getElementById('some_frame_id')。contentWindow」。所以你可能也可以這樣訪問文檔。 – 2014-10-09 10:05:03

+0

@PeterBranforn我也試過這個,因爲我在其他地方使用,如果在這裏檢查,但沒有運氣。 – 2014-10-09 10:10:09

回答

2

試試這個。它應該工作。

var _script = document.createElement('script'); 
      _script.type = 'text/javascript'; 
      var iFrame = document.getElementById('fr'), 
      iFrameBody, 
      t = document.getElementById("inpt"), 
      btn = document.getElementById('btn'); 

     function injectJS(content, val) { 
      if (iFrame.contentDocument) 
      { 
      iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0]; 
      } 
      else if (iFrame.contentWindow) 
      { 
      iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0]; 
      } 
      iFrameBody.appendChild(content); 
      content.innerHTML = val; 
      console.log(iFrameBody);  
     } 

     //btn.onclick = injectJS(_script, t.value); 
     btn.addEventListener("click",(function(){injectJS(_script, t.value);}),false); 
+0

@ os-web感謝它的運作。 – 2014-10-09 10:40:31

0

您不能將文本附加到script標記,並且需要將onclick分配給元素本身。

如果onclick未在HTML中關聯,則會在加載頁面時觸發onclick事件。

我已對Fiddle進行了更改。

+0

請檢查http://jsfiddle.net/n5fdywb9/3/ !! .. – 2014-10-09 10:53:48

+0

這是拋出一個錯誤。 – Aashray 2014-10-09 10:55:10

+0

您正在使用哪個瀏覽器..:/ – 2014-10-09 10:55:58

0

通用腳本的addEvent ..

 <textarea id="inpt" cols="30" rows="10"></textarea> 
<div><input type="submit" value="append" id="btn" style="width:60px;height:20px;background:grey"></div><br> 
<iframe id="fr" name="fr" style="width:300px;height:300px;"></iframe> 
<script type="text/javascript"> 
document.getElementById('inpt').innerHTML = "document.write('awesome')"; 
if (typeof addEventListener !== "undefined") { 
    addEvent = function (obj, evt, fn) { 
     obj.addEventListener(evt, fn, false); 
    }; 

    removeEvent = function (obj, evt, fn) { 
     obj.removeEventListener(evt, fn, false); 
    }; 
} else if (typeof attachEvent !== "undefined") { 
    addEvent = function (obj, evt, fn) { 
     var fnHash = "e_" + evt + fn; 

     obj[fnHash] = function() { 
      var type = event.type, 
       relatedTarget = null; 

      if (type === "mouseover" || type === "mouseout") { 
       relatedTarget = (type === "mouseover") ? event.fromElement : event.toElement; 
      } 

      fn.call(obj, { 
       target: event.srcElement, 
       type: type, 
       relatedTarget: relatedTarget, 
       _event: event, 
       preventDefault: function() { 
        this._event.returnValue = false; 
       }, 
       stopPropagation: function() { 
        this._event.cancelBubble = true; 
       } 
      }); 
     }; 

     obj.attachEvent("on" + evt, obj[fnHash]); 
    }; 
} 
var _script = document.createElement('script'); 
_script.type = 'text/javascript'; 
var iFrame = document.getElementById('fr'), 
    iFrameBody, 
    t = document.getElementById("inpt"), 
    btn = document.getElementById('btn'); 

function injectJS(content, val) { 
    removeJS(); 
    var windowVal; 
    if (iFrame.contentDocument) { 
     windowVal = iFrame.contentDocument; 
     iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0]; 
    } else if (iFrame.contentWindow) { 
     windowVal = iFrame.contentWindow; 
     iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0]; 
    } 
    content.innerHTML = val; 
    iFrameBody.innerHTML = ""; 
    iFrameBody.appendChild(content);  
    if(isScriptTagIsThere()){  
     windowVal.location.href = "javascript:" + val; 
    } 
} 
function isScriptTagIsThere(){ 
    if (iFrame.contentDocument) { 
     iFrameScript = iFrame.contentDocument.getElementsByTagName('script')[0]; 
    } else if (iFrame.contentWindow) { 
     iFrameScript = iFrame.contentWindow.document.getElementsByTagName('script')[0]; 
    } 
    if (iFrameScript) { 
     return true; 
    }else{ 
     return false; 
    } 
} 
function removeJS() { 
    if (iFrame.contentDocument) { 
     iFrameScript = iFrame.contentDocument.getElementsByTagName('script')[0]; 
    } else if (iFrame.contentWindow) { 
     iFrameScript = iFrame.contentWindow.document.getElementsByTagName('script')[0]; 
    } 
    if (iFrameScript) { 
     iFrameScript.parentNode.removeChild(iFrameScript); 
    } 
} 
addEvent(btn, "click", (function() { 
    injectJS(_script, t.value); 
}), false); 

http://jsfiddle.net/n5fdywb9/6/

讓我知道如果它幫助..

+1

@ rahul-send89它完美的作品。謝啦! – 2014-10-09 10:37:45

+0

歡迎您! – 2014-10-09 10:40:02

+0

@ rahul-send89當你第二次運行iframe中的JavaScript時,只有一個問題會出現。第一次運作。我修改了你的小提琴,但仍然無法工作。檢查http://jsfiddle.net/n5fdywb9/5/ – 2014-10-09 18:12:56