【问题标题】:Passing a variable in script src在脚本 src 中传递变量
【发布时间】:2019-06-30 09:51:55
【问题描述】:

我有以下代码:

<div class='trick' id='1234'>
  &lt;script src=&quot;/feeds/posts/default?max-results=50&amp;orderby=published&amp;alt=json-in-script&amp;callback=maps_location&quot;&gt;&lt;/script&gt;
</div>

我正在尝试将其替换为:

<div class='trick' id='1234'>
      &lt;script src=&quot;/feeds/posts/default/-/variableB?max-results=50&amp;orderby=published&amp;alt=json-in-script&amp;callback=maps_location&quot;&gt;&lt;/script&gt;
    </div>

我希望 variableB 等于 last_segment 变量的地方

<script type='text/javascript'>
var segment_str = window.location.pathname;
var segment_array = segment_str.split( &#39;/&#39; );
var last_segment = segment_array.pop();
</script>

所以,我想做的是在一个当前的网址中,比如http://XXXXXXX.blogspot.com/search/label/test

只为标签“test”提供回调提要

有可能吗?

【问题讨论】:

  • 是的,有可能,不管怎样。
  • 我不这么认为,因为您没有将其转换为 html 或解析 JavaScript。
  • 如何传递这个变量?我找到了一种将 URL 作为变量传递的方法,在脚本 Id 上给出......
  • 赞这个stackoverflow.com/q/40198577/2069177但是没有结果...

标签: javascript json label feed


【解决方案1】:

我找到了解决方案....效果很好...

<div class='trick' id='1234'>
<script>
var segment_str = window.location.pathname; // return segment1/segment2/segment3/segment4
var segment_array = segment_str.split( &#39;/&#39; );
var last_segment = segment_array.pop();

                              document.write(&quot;&lt;div id=\&quot;12345\&quot;&gt;&lt;script src=\&quot;https:\/\/tinosemagazine-new.blogspot.com/feeds/posts/default/-/&quot;+last_segment+&quot;?max-results=100&amp;orderby=published&amp;alt=json-in-script&amp;callback=maps_location\&quot;&gt;&lt;\/script&gt;&lt;\/div&gt;&quot;);
                            </script>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多