简体   繁体   English

如何在页面Javascript准备好处理它们之前禁用AJAX-y链接?

[英]How to disable AJAX-y links before page Javascript ready to handle them?

I am implementing a shopping cart for my website, using a pseudo-AJAX Lightbox-esque effect. 我正在使用伪AJAX灯箱式效果为我的网站实现购物车。 (It doesn't actually call the server between requests -- everything is just Prototype magic to update the displayed values.) (实际上并没有在两次请求之间调用服务器-一切都只是原型魔术来更新显示的值。)

There is also semi-graceful fallback behavior for users without Javascript: if they click add to cart they get taken to an (offsite, less-desirable-interaction) cart. 对于没有Javascript的用户,还存在半优美的后备行为:如果他们单击添加到购物车,他们将被带到(异地,不良互动)购物车。

However, a user with Javascript enabled who loads the page and then immediately hits add to cart gets whisked away from the page, too. 但是,启用了Javascript的用户会加载页面,然后立即将点击​​添加到购物车,也会从页面中消失。 I'd like to have the Javascript just delay them for a while, then execute the show cart behavior once it is ready. 我想让Javascript延迟一会儿,然后在准备就绪后执行show cart行为。 In the alternative, just totally ignoring clicks before the Javascript is ready is probably viable too. 另外,在Javascript准备就绪之前完全忽略点击也可能是可行的。

Any suggestions? 有什么建议么?

I now do this with jQuery b/c I vaguely recall browser differences which jQuery takes care of: 我现在使用jQuery b / c来做到这一点,我隐约回忆起jQuery处理的浏览器差异:

Try 尝试

$(document).ready(function() { // put all your jQuery goodness in here. }); $(document).ready(function(){//将所有jQuery优点放入此处。});

Is your code really that slow that this is an issue? 您的代码真的慢到有问题吗? I'd be willing to bet that no one is going to be buying your product that soon after loading the page. 我敢打赌,加载页面后不久,没人会购买您的产品。 In any reasonable case, the user will wait for the page to load before interacting with it, especially for something like a purchase. 在任何合理的情况下,用户都将等待页面加载后再与页面进行交互,尤其是对于购买之类的东西。

But to answer your original question, you can disable the links in normal code, then reenable them using a document.observe("dom:loaded", function() { ... }) call. 但是要回答您的原始问题,您可以禁用普通代码中的链接,然后使用document.observe("dom:loaded", function() { ... })调用来重新启用它们。

You can try, hiding the links in css then show them when the page loads. 您可以尝试在CSS中隐藏链接,然后在页面加载时显示它们。

<script type="text/javascript">
  document.write('<link rel="stylesheet" type="text/css" href="someCssThatHidesLinks.css" />');

  window.onLoad = function() {
    //show links
  }

</script>

This way, if your user doesn't have javascript, then their links are still active, otherwise if they do, then the links are hidden until you load and activate them. 这样,如果您的用户没有javascript,则他们的链接仍处于活动状态,否则,它们将被隐藏,直到您加载并激活它们。 Never done this before, but i think this should work if you want to retain the features of your failsafe page. 以前从未做过此操作,但是我想如果您想保留故障安全页面的功能,则应该可以使用。

However, a user with Javascript enabled who loads the page and then immediately hits add to cart gets whisked away from the page, too. 但是,启用了Javascript的用户会加载页面,然后立即将点击​​添加到购物车,也会从页面中消失。

When are you starting up your scripts? 您什么时候启动脚本? If you're using document onload, it will be waiting for all the images to download before initialising, which would indeed give the user a chance to click-to-buy. 如果您使用的是onload文档,它将在初始化之前等待所有图像下载,这确实使用户有机会点击购买。

If you trigger the JS enhancement when the DOM is ready, either by just putting your <script> at the bottom of the page, or by using a DOMContentLoaded-style event supplied by your framework, the links should adapt fast enough that the user is very unlikely to be clicking the button first. 如果在DOM准备就绪时触发JS增强功能,只需将<script>放在页面底部,或者使用框架提供的DOMContentLoaded样式事件,则链接应足够快地适应用户不太可能先单击该按钮。

If you really, really want to delay clicks that are placed between the element first being parsed, and the document being loaded, you could write something like: 如果您确实希望延迟第一次解析的元素与加载的文档之间的单击,则可以编写如下内容:

<script>
    function methodcall(obj, name) {
        return function() {
            obj[name].call(obj);
        };
    }
</script>
...
<a href="nonjsversion" onclick="setTimeout(methodcall(this, 'click'), 500); return false;">Buy</a>
...

So that it'd just spin there polling (and, on IE, leaking memory) until the link's real click handler was in place. 这样它就可以旋转查询(并且在IE上泄漏内存),直到链接的实际单击处理程序就位为止。 It's pretty ugly though, and fragile in that if your script breaks for some reason (and it's JavaScript, so it's pretty likely that at some point, on some browser, it will), your button will break. 不过这很丑陋,而且很脆弱,如果您的脚本由于某种原因而中断(它是JavaScript,因此很可能在某些时候在某些浏览器上会中断),则您的按钮将会中断。 This problem applies to all potential solutions that rely on a later-running JavaScript enabling previously-disabled actions. 此问题适用于所有依赖较新版本的JavaScript的潜在解决方案,这些JavaScript启用了以前禁用的操作。

And there's nothing worse for business than a broken buy button. 对企业而言,没有什么比破损购买按钮更糟糕的了。

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

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