我有3页正在使用。

shop_mobile.php

该页面从MySQL数据库检索产品数据并列出其格式。它接受网址参数brand = BRANDNAME和cat = CATEGORY。

这是此页面的两个示例:
http://solanocycle.com/shop_mobile.php?brand=KYMCO&cat=KYMCO%20Scooters

http://solanocycle.com/shop_mobile.php?brand=peacesports&cat=Peace%20Sports%20Scooter

Vehicles.html

该页面包含用于查看特定产品列表页面的链接(即“ KYMCO踏板车”,“和平运动踏板车”)。单击这些链接后,它们都应将用户带到同一页面(view.html),但传递URL参数,这些参数告诉view.html中的iframe哪个URL用作其来源。

view.html

该页面包含一个Iframe,该Iframe的来源是来自Vehicles.html链接所传递的URL参数

注意:

我尝试使用How do I pass URL parameter to an iFrame in Wordpress page?提供的解决方案来解决我的问题,但是我以前从未使用过jquery,因此无法使其正常工作。

任何帮助将不胜感激。

更多信息

来自Vehicles.html的链接示例

<a href="view.html?http://solanocycle.com/shop_mobile.php?brand=KYMCO&cat=KYMCO%20Scooters">KYMCO Scooters</a>

我认为View.html应该做什么:


从URL“ view.html?http://solanocycle.com/shop_mobile.php?brand=KYMCO&cat=KYMCO%20Scooters”中提取“ http://solanocycle.com/shop_mobile.php?brand=KYMCO&cat=KYMCO%20Scooters”,并将其存储为名为$ embedLink的变量。
显示以$ embedLink作为其源网址的iframe。

最佳答案

假设您在Vehicles.html上的链接结构合理,如下所示:

view.html?url = http://solanocycle.com/shop_mobile.php?brand = KYMCO&cat = KYMCO%20Scooters

view.html所需的jQuery将是:

<script type="text/javascript">
$(document).ready(function(){
    var regex = /\?url=(.*)/,
    $embedLink = document.location.href.match(regex)[1];
    $('iframe').attr('src', $embedLink);
});
</script>


显然,您可以将“ iframe”替换为iframe的ID或类,以使其更加具体。

另一种方法是使用php提取请求参数,并回显它代替iframe的src属性:

<?php $embedLink = (isset($_GET['url']) ? $_GET['url'] : ''); ?>

<iframe src="<?php echo $embedLink; ?>"></iframe>


我建议在两种情况下都对请求参数进行清理,以防止XSS。

08-07 09:02