设置src =“id”iframe通过url.com/id获取src

时间:2016-10-11 15:49:29

标签: javascript html iframe youtube

我尝试使用工具栏地址中的url设置iframe的src。

地址如果由域+ id(与youtube id相同)

组成

示例 www.youtube.com/whatch=id

我的域名是= www.domain.com/?id(ID是相同的)

这是我假设的剧本

<script type="text/javascript">
   var segment_str = window.location.pathname; 
   var segment_array = segment_str.split( '/' );
   var last_segment = segment_array[segment_array.length - 1];
   alert(last_segment);
   var iframe = document.getElementById("youtube");
   iframe.src = www.youtube.com/watch=last_segment;
   </script>

1 个答案:

答案 0 :(得分:1)

Maurizio Grasso!
@LGSon几乎是正确的 - 你必须在设置iframe.src时使用引号。

但是,如果您的域名是www.domain.com/?id(带问号),则必须使用var id = window.location.search.slice(1); iframe.src = 'www.youtube.com/watch=' + id; 来获取网址参数而不是// Create the XHR object. function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("withCredentials" in xhr) { // XHR for Chrome/Firefox/Opera/Safari. xhr.open(method, url, true); } else if (typeof XDomainRequest != "undefined") { // XDomainRequest for IE. xhr = new XDomainRequest(); xhr.open(method, url); } else { // CORS not supported. xhr = null; } return xhr; } // Make the actual CORS request. function makeCorsRequest() { // This is a sample server that supports CORS. var url = 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html'; var xhr = createCORSRequest('GET', url); if (!xhr) { alert('CORS not supported'); return; } // Response handlers. xhr.onload = function() { var text = xhr.responseText; alert('Response from CORS request to ' + url + ': ' + text); }; xhr.onerror = function() { alert('Woops, there was an error making the request.'); }; xhr.send(); } ,因此您的脚本会变成类似:

username = yourUsername
password = yourPassword
domain = yourDomain