gs038538
V2EX  ›  问与答

php 下获取 url 值通过 js 处理后,如何显示在 html 的 iframe 中呢?

  •  
  •   gs038538 · May 4, 2015 · 3365 views
    This topic created in 4100 days ago, the information mentioned may be changed or developed.
    需要显示的位置已经在代码那指明,请各路高人指点一二。

    url取值方法如下: http://xxx.com/test.php?v=/xxx/xxx.mp3


    代码分割线
    ------------------------------------------------------------------------------------------


    <?php
    error_reporting(0);
    header("Content-Type: text/html; charset=utf-8");
    $v=$_GET["v"];
    ?>
    <script type="text/javascript">
    var url = '<?php echo $v ?>';
    url = url.replace(/\//g,'%252F');
    </script>


    <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path='这里需要显示js处理后的值,跟前面的部分组成完整的url'" scrolling="no" frameborder="0"></iframe>


    代码分割线
    ------------------------------------------------------------------------------------------


    期望的显示结果为
    <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path=%252Fxxx%252Fxxx.mp3" scrolling="no" frameborder="0"></iframe>
    11 replies    2015-05-04 14:51:02 +08:00
    ituren
        1
    ituren  
       May 4, 2015
    $('iframe.xxxxx').prop('src',$('iframe.xxxxx').prop('src')+url);
    gs038538
        2
    gs038538  
    OP
       May 4, 2015
    @ituren 方便给一个相对完整的代码吗?感谢您。
    ituren
        3
    ituren  
       May 4, 2015   ❤️ 1
    @gs038538 那要看你iframe的这段代码是怎么生成的,你方便贴你的完整代码么?
    gs038538
        4
    gs038538  
    OP
       May 4, 2015
    @ituren <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path='这里需要显示js处理后的值,跟前面的部分组成完整的url'" scrolling="no" frameborder="0"></iframe>

    这段代码除了js那是变量外,其余部分是固定的,直接就写到了php里 保持html的输出即可。
    现在的做法就是直接写在了php里。帖子那就是我的完整代码啦~~不需要生产iframe。
    谢谢您!
    b821025551b
        5
    b821025551b  
       May 4, 2015
    $('iframe.xxxxx').prop('src',$('iframe.xxxxx').prop('src')+'这里需要显示js处理后的值,跟前面的部分组成完整的url');
    如果多个iframe用到xxxxx,那就给个id吧。
    ituren
        6
    ituren  
       May 4, 2015
    @gs038538 那你直接用PHP拼接输出就得了, 干嘛还要用JS.
    gs038538
        7
    gs038538  
    OP
       May 4, 2015
    @b821025551b 这一句需要放在哪个位置呢?

    需要的结果是

    <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path=%252Fxxx%252Fxxx.mp3" scrolling="no" frameborder="0"></iframe>

    其中
    %252Fxxx%252Fxxx.mp3
    是被js处理过的~
    原值为/xxx/xxx.mp3

    这部分:
    http://static.xxx.com/video?outlink_code=9f7144ed&path=
    是固定的。

    您跟ituren指点的方法是一样的,但是我不知道如何去使用,见笑了。谢谢。
    gs038538
        8
    gs038538  
    OP
       May 4, 2015
    @ituren 是哦~~
    php需要处理 /xxx/xxx.mp3 替换字符串/为%252F %252Fxxx%252Fxxx.mp3
    然后放到 iframe的 “ http://static.xxx.com/video?outlink_code=9f7144ed&path= 替换的结果 ”...
    然后我又不知道怎么把变量传过去了。
    ituren
        9
    ituren  
       May 4, 2015   ❤️ 1
    @gs038538
    echo '<iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path='.$v.'" scrolling="no" frameborder="0"></iframe>';

    不用转换斜杠
    gs038538
        10
    gs038538  
    OP
       May 4, 2015
    @ituren 解决了!谢谢~~ '.$v.' 只需要这样就可以了!完全可以百度查到,打扰您了。谢谢。
    msg7086
        11
    msg7086  
       May 4, 2015
    你这个问题问得不对,主要是因为在你的网页上,PHP是比JS先执行的。也就是说,在执行到你这个iframe的时候,上头的js代码还未开始执行呢。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   923 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:34 · PVG 03:34 · LAX 12:34 · JFK 15:34
    ♥ Do have faith in what you're doing.