me-me
me-me

Reputation: 5829

document.location.hash not working in Safari

I'm using $postMessage to communicate cross browser with the parent window. Everything works fine in all browsers except Safari. The issue is using document.location.hash The hash comes back as empty in Safari. Anyway around this happening in Safari.

function postMessage( msg ){  
  var parent_url = decodeURIComponent( document.location.hash.replace( /^#/, '' ) );
    $.postMessage({ method:'resize', message:msg}, parent_url, parent );
} 

Update:

So the parent window is creating an Iframe which appends '#url to the end of it. I'm using document.location.hash.replace( /^#/, '' ) to grab that url to use as the url for postMessage. When testing all this in Safari V8.0.2 Safari was removing the # and anything after the #. So my parent_url was coming back blank.

The solution to this was to use a &param=url at the end of the url instead of the #. I'm not sure why Safari would be removing the # or if it's a well documented issue. I have been searching online to find out more info.

Upvotes: 3

Views: 4824

Answers (2)

krschn
krschn

Reputation: 183

Just ran into this as well with Safari on an old iPad that I cannot update beyond iOS 12.5.1. I have a small Web app which uses Spotify implicit grant for authorization. Spotify returns the access token in a hash fragment (#access_token...), appended to my redirect URI. This works fine in all browsers except Safari on this old iPad. Safari works fine in newer versions of iOS. So I hooked up Safari on my Mac using its Web Inspector to my iPad and found that both document.location.hash and window.location.hash are empty. So seems like there's no way to get Safari on older iOS versions working with any APIs (like Spotify implicit grant) that use hash fragments when redirecting back.

Upvotes: 0

Wex
Wex

Reputation: 15715

If location.hash is not available, why not use location.href?

href    = location.href;
hashIdx = href.lastIndexOf('#');
hash    = href.slice(hashIdx);

// or in one line
hash = location.href.slice(location.href.lastIndexOf('#'))

Note that hash is not supported in quite a few browsers: https://developer.mozilla.org/en-US/docs/Web/API/URLUtils.hash

Upvotes: 2

Related Questions