Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - $window.location.origin gives wrong value when using IE

$window.location.origin returns the wrong value on IE.

The origin property returns the protocol, hostname and port number of a URL.

Example

url: http://localhost:8080/products/search

Chrome: $window.location.origin returns http://localhost:8080

IE: $window.location.origin returns http://localhost:8080/products/search

How can I have the right value on IE?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You may also need the port number. If so, you can use this polyfill

if (!window.location.origin) {
  window.location.origin = window.location.protocol + "//" 
    + window.location.hostname 
    + (window.location.port ? ':' + window.location.port : '');
}

This polyfill is already part of Modernizr.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...