简体   繁体   English

IE11:将 window.location.origin 复制到剪贴板无法正常工作

[英]IE11: Copying window.location.origin to clipboard doesn't work correctly

I have a function that's supposed to copy window.location.origin to the clipboard for the user to paste:我有一个函数应该将window.location.origin复制到剪贴板供用户粘贴:

  <OptionButton
    onClick={() => this.copyLink(`${window.location.origin}/calc/${apiId}/${formatScenarioName(database.api.scenarioName || database.inputs.scenarioName)}`)}
  >
    Copy Link
  </OptionButton>



  copyLink(value) {
    const tempInput = document.createElement('input')
    tempInput.style = 'position: absolute; left: -1000px; top: -1000px'
    tempInput.value = value
    document.body.appendChild(tempInput)
    tempInput.select()
    document.execCommand('copy')
    document.body.removeChild(tempInput)
    message.success('Link copied to clipboard!')
  }

While this works on any other browser, this fails on IE11.虽然这适用于任何其他浏览器,但在 IE11 上却失败了。 I've tried incorporating react router, but the requirement is to have the full link rather than just the params.我试过结合反应路由器,但要求是有完整的链接,而不仅仅是参数。 However a workaround I've also tried is plainly adding window.location.href , but that's not very dynamic.然而,我也尝试过的一种解决方法是简单地添加window.location.href ,但这不是很动态。

Is there a polyfill for this on IE11?在 IE11 上有这个 polyfill 吗? Or a workaround to this?或者解决这个问题?

This rang a little bell when I've searched for a polyfill in the past.当我过去搜索 polyfill 时,这敲响了警钟。

Something I use throughout my code base, so you might need small adaptations to fit in React most likely:我在整个代码库中都使用了一些东西,因此您可能需要进行一些小的调整以适应 React:

/**
  * Polyfill for window.location
  * @inner {string} origin fix IE11 on windows 10 issue
  * @inner {string} hash fix # inconsistency
  * @return {Object<Location>} window.location
  * @see https://connect.microsoft.com/IE/feedback/details/1763802/location-origin-is-undefined-in-ie-11-on-windows-10-but-works-on-windows-7
  * @see https://stackoverflow.com/questions/1822598/getting-url-hash-location-and-using-it-in-jquery
  */
    location: (function (loc) {
        return loc.origin
            ? loc
            : (function () {
                var origin =
                    loc.protocol + '//' + loc.hostname + (loc.port ? ':' + loc.port : ''),
                    hash = loc.hash.replace('#', '');

                try {
                    Object.defineProperty(loc, {
                        origin: {
                            value: origin,
                            enumerable: true
                        },
                        hash: {
                            value: '#' + hash,
                            enumerable: true
                        }
                    });
                } catch (e) {
                    loc.origin = origin;
                    loc.hash = hash;
                }

                return loc;
            })();
    })(window.location),

Your error is coming from the style assignment.您的错误来自样式分配。

tempInput.style = 'position: absolute; left: -1000px; top: -1000px'

In 'strict' mode you shouldn't be able to write multiple values to style using css text format.在“严格”模式下,您不应该使用 css 文本格式将多个值写入样式。 Most browsers don't enforce this, but IE 11 does.大多数浏览器不会强制执行此操作,但 IE 11 会强制执行。

I suspect your console reports this as:我怀疑您的控制台将此报告为:

SCRIPT5045 Assignment to read-only properties is not allowed in strict mode SCRIPT5045 在严格模式下不允许分配给只读属性

If you were manipulating an element that already had styles, you might want to set each property separately, but there is a way to just completely replace any existing styles.如果您正在操作一个已经有样式的元素,您可能希望单独设置每个属性,但有一种方法可以完全替换任何现有样式。 Because you are setting this on a temporary element you can use cssText:因为您是在一个临时元素上设置它,所以您可以使用 cssText:

tempInput.style.cssText = 'position: absolute; left: -1000px; top: -1000px'

That should be compatible across browsers.这应该是跨浏览器兼容的。

For further reference see ElementCSSInlineStyle.style如需进一步参考,请参阅ElementCSSInlineStyle.style

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM