设置location.hash的值,前面不要带#号,为了兼容Safari

本文介绍了一种在Safari浏览器中正确设置URL hash值的方法。通过避免在location.hash属性赋值时带上'#'符号,可以确保在不同浏览器间的一致性。
width="760" scrolling="no" height="90" frameborder="0" src="http://p.alimama.com/code.php?t=2&i=mm_10003521_106784_109301&w=760&h=90&sz=11&bgc=FFFACD&bdc=FFFFFF&tc=000080&lc=000000&dc=000000" name="alimamaifrm" id="alimamaifrm" border="0" marginwidth="0" marginheight="0">
在网页http://xxx.com//a.html#abc中
alert(location.hash)得到的结果是  #abc
如果想改变hash值,理所当然应该是location.hash = "#xyz";
但是这句话在safari浏览器上出了差错:
在safari下网页的url会变成http://xxx.com//a.html#%23xyz
而%23即是转码之后的#

所以设置hash值时前面不要带#号
location.hash = "xyz";
这样在各种浏览器中都会得到想要的结果

safari的雷真多,都老子有钱了,买个2手mac,好好修理修理它
 
下面这段代码在代码检视时出现了报错: const sharedClick = async (event) => { preventPropagation(event, fromPage); // 调用阻止冒泡的函数 event.preventDefault(); // 阻止冒泡以防止a标签中超链接跳转 setShares(shares + 1); // 分享总数+1 shareTrack && shareTrack(); let url = getUrl(); h5CommonFunctionTrack({ data: { function: '13', pageType: '详情页', columnType, loginUser } }); let title = parseTopic(detailData.title, true) || dictWX[columnType] || dictWX.pageName; if (window.location.href.includes('/datasets')) title = parseTopic(datasetsDict.shareTitle, true); let text = title + ' ' + url; if ( browser === 'Safari' && (window.location.href.includes('2024AnnualWrap-up') || window.location.href.includes('2025AnnualWrap-up')) ) text = text + ' ' + dictWX.pageName; if (!(window.App && window.App.openShare) && !isApp) { // IOS:IOS系统分享 启用IOS系统所的分享功能 if ([isIos, navigator.share].every(Boolean)) { // 当浏览器支持此功能// 要共享的 URL// 要共享的文本text:推荐理由 or 摘要 or 正文 // 要共享的标题title share({ title, url, text }); } else { copy(title + ' ' + url); Toast.show({ content: dict.copyToClipboard }); } return; } // 兼容(IOS)welink分享不能有换行 let summary = getPlainText(detailData.content) ?.replace(/"/g, '"') ?.replace(/\\/g, '\') .replace(/\t|\n|\v|\r|\f/g, '') .substring(0, 100) || ''; // 转义话题 summary = parseTopic(summary, true); if (isApp) { const webUrl = isHarmonyos ? window.location.href : url; let argumentsP = { ...{ title: parseTopic(detailData.title?.replace(/"/g, '"') || '', true), webUrl, summary }, ...{ imgUrl: shareCover || defaultImg, contentId }, }; if (window.location.hash.includes('/datasets')) { argumentsP = { ...{ title: parseTopic(datasetsDict.shareTitle?.replace(/"/g, '"') || '', true), webUrl, summary: parseTopic(datasetsDict.shareDesc, true), }, ...{ imgUrl: datasetsDict.shareImgUrl || defaultImg, contentId }, }; } appCall({ funcName: 'share', arguments: argumentsP, callback: 'cswAppCallbackOnShare' }); window.cswAppCallbackOnShare = () => {}; // setShares(shares + 1); //// 【待app实现callback】默认app分享成功+1, +1接口请求在app实现 } else { if (window.location.hash.includes('/datasets')) { return window.App.openShare( parseTopic(datasetsDict.shareTitle, true), window.location.href, datasetsDict.shareImgUrl, parseTopic(datasetsDict.shareDesc, true), () => {} ); } const req = await getshareIconPermission(contentId); let metaImage = req?.data?.shareIcon; window.App.openShare( parseTopic(detailData.title.replace(/"/g, '"'), true), window.location.href, metaImage, summary, () => {} ); } };Line216. Async arrow function expected no return value.
最新发布
12-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值