在脚本src中传递变量

时间:2019-06-30 09:51:55

标签: javascript json label feed

我有以下代码:

<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>

我希望变量B等于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”的回调供稿

有可能吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。。。它工作得很好...

<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>