【问题标题】:Display part of URL on a page (i.e. Instagram Access Token)?在页面上显示部分 URL(即 Instagram 访问令牌)?
【发布时间】:2014-05-03 15:48:52
【问题描述】:

我想要实现的是当用户通过 Instagram 进行身份验证时,他们被重定向到“http://domainnamehere./instagram#access_token=27744335.fcaef44.a95s9e741f3c4ea7bcad21d80809ca40”之类的页面

为了能够在网页的一部分上打印“27744335.fcaef44.a95s9e741f3c4ea7bcad21d80809ca40”字符串,即 <p>Here is your access token: ________</p>

我知道如何使用 javascript 打印整个 URL,使用这个: <h3 id="right"> <script type="text/javascript"> document.write(location.href); </script> </h3>

但是如何设置它以仅获取 URL 的一部分?

我想要达到的目标可以在这里实时看到http://theultralinx.com/instagram(点击“设置 instagram”,进行身份验证,然后下一页显示访问令牌。

谢谢!

【问题讨论】:

  • 任何答案对您有用吗?如果是,请接受其中一个答案。

标签: javascript php instagram


【解决方案1】:

您仍然可以查找 URI 解析器,但可以使用基本的字符串函数(如 indexOfsubstring,如下所示:

var uri = "http://domainnamehere./instagram#access_token=27744335.fcaef44.a95s9e741f3c4ea7bcad21d80809ca40";

var indexOfHashVar = uri.indexOf("access_token=");
// 13 is the number of characters of "access_token="
var token = uri.substring(indexOfHashVar + 13);
console.log(token);

正如@PhistucK 在一些评论中建议的那样,您可能会使用window.location.hash 获得整个uri 的哈希部分,因此我的示例可能会在第一行更改为:

var uri = window.location.hash;

【讨论】:

  • 使用document.location.hash而不是整个URL会更高效、更容易。
  • @PhistucK 你是对的,但我只是展示了如何提取通用字符串的一部分......现在我没有时间把你的想法放在我的问题中,你可以编辑它,否则我会在几个小时内完成 :) 感谢您的建议
【解决方案2】:

永远不要使用document.write,这是不鼓励的。 这应该可以。

HTML -

<p>Here is your access token: <span id="access-token"></span></p>

JavaScript -

window.addEventListener("load", function () {
 // Getting the part of the URL that starts with #.
 var hash = document.location.hash,
     // Storing the string for which we search in the hash.
     accessTokenParameter = "access_token=";
 // Setting the found value as the content of the access-token element we created
 // above by taking the part of the hash (substring) that begins after the
 // parameter by getting the index of the character (indexOf) with which the
 // parameter name starts and adding the length of the parameter name (and =)
 // in order to get past the name and start from the value.
 document.getElementById("access-token").innerHTML =
  hash.substring(hash.indexOf(accessTokenParameter) +
   accessTokenParameter.length);
}, false);

为了也支持 Internet Explorer,您可以使用window.onload = 或命名函数并在特征检测后使用window.attachEvent

【讨论】:

  • 您好,感谢您添加 cmets 并解释其工作原理。但是,当我尝试这样做时,我的浏览器告诉我最后一行有错误。 “ReferenceError:赋值左侧不是参考。”
  • 对不起!你说得对,我忘了加.innerHTML。固定。
  • 嗨,现在我得到:TypeError: 'null' is not an object (evalating 'document.getElementById("access-token").innerHTML = hash.substring(hash.indexOf(accessTokenParameter ) + accessTokenParameter.length)')
  • 你使用了我写的 HTML 吗?它有一个具有此“访问令牌”ID 的元素。
  • 是的,我做到了。我添加了 javascript 内联...让我在头脑中尝试一下?编辑:在头部尝试过,仍然没有骰子。那是 Safari 的调试日志向我显示的错误。我正在使用包裹在段落标签中的&lt;span id="access-token"&gt;&lt;/span&gt;
【解决方案3】:
<h3 id="right">
<script type="text/javascript">document.write(window.location.hash.split("access_token=")[1]);  </script>
</h3>

【讨论】:

    【解决方案4】:

    你可以用这个

    <h3 id="right">  <script type="text/javascript"> var href = location.href; href = href.replace("http://domainnamehere./instagram#access_token=","");  document.write(href);  </script>  </h3>
    

    【讨论】:

    • 最轻微的更改可能会破坏这一点,例如,从 URL 中删除 www(在大多数情况下有效)。
    • 这行得通!唯一的事情是我必须创建一个新的重定向页面。谢谢。
    【解决方案5】:

    如果你的链接总是像问题中的链接(相同的域且没有其他变量)你可以使用这个:

    var url =location.href ;
    var res = url.slice(45);
    console.log(res);
    

    【讨论】:

    • 最轻微的更改可能会破坏这一点,例如,从 URL 中删除 www(在大多数情况下有效)。
    • 这是重定向后的 url 并使用开发人员给定的 url 未键入的 url
    • 给我一个不使用document.location.hash的充分理由吗?
    • 他要求回答,我告诉他我知道的方式,使用这种方式没有任何作用,也不需要给你一个理由使用这种方式而不是document.location.hash
    • 坦率地说,出于礼貌,我只是写下了投反对票的原因。随心所欲。
    猜你喜欢
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    相关资源
    最近更新 更多