Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

javascript - On my webpage, using "window.location.href" returns "about:srcdoc" instead of the URL name

To see the problem, enter this URL "https://gainesrad.com/readqrcodefromkiosk/"

It should display the URL of the webpage but instead I get "about:srcdoc".

Here is the HTML code on gainesrad.com/readqrcodefromkiosk

<!DOCTYPE html>
<html>
<body>
<b>This should tell me the URL of the page but instead is says "about:srcdoc"</b>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "The full URL is:  " + window.location.href;
</script>
</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The srcdocattribute is used in Iframe context to indicate the content of an iframe. While inspecting the full source code of your page, I noticed you are using an iframe, and that's why you are getting about:srcdoc with window.location.href

To get the URL move the inline <script> to the document context outside the Iframe.

See this: https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/srcdoc

Since your iframe is contained within the Document context, you can do this to retrieve the base URI of the containing document: https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentDocument

   let iframe = document.querySelector('iframe')
   console.log('The baseURI is: ' + iframe.contentDocument.baseURI)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...