-1

我想根据 URL 用 javascript 更改 iframe 的大小,

我的意思是,如果网址是:

mywebpage.com/example.html?width=800&height=450

iframe 是

<iframe width="800" height="450" scrolling="no" frameborder="0" allowtransparency="true" marginwidth="0" marginheight="0" src="http://mips.tv/embedplayer/test121/1/800/450"></iframe>

看到 iframe 中的宽度和高度值各为两倍

我在这里有一个例子,但它现在在 iframe 的 src 值中工作

http://redzeronline.ucoz.com/xd.html?width=800&height=450

4

1 回答 1

0

'/' 字符保留用于定位文件夹。如果您使用查询会更好。

正则表达式对查询很有用。

//where frame is the iframe you showed above:
var query = JSON.parse(('{'+(frame.source.substr(1).replace(new RegExp("([^=]+)=([^&]+)&?",'g'),function(){return '"'+arguments[1]+'":"'+arguments[2]+'",';}))+'}').replace(/,\}/,"}"));

无论如何,我想您也可以将正则表达式用于您想要的格式。我只是强烈建议您使用查询字符串。

var width = -1;
var height = -1;
...
//frame is the iframe that you show.
//You should consider adding an id attribute to that so that you can identify it later.
//It's good coding practice.
var results = frame.match(/([0-9]+)\/([0-9]+)$/);
if(results.length == 3)
{
   width=parseInt(results[1]);
   height=parseInt(results[2]);
}
else
{
   //something went wrong...
}

如果要更改帧大小,只需调用replace()而不是match(). 确保按照EcmaScript 标准中的描述传递函数。

要了解有关 JavaScript 中正则表达式的更多信息,请参阅此网页

于 2013-06-25T22:03:38.760 回答