Onclick to open without clicking

I have that button to open a script>

<button onClick="ddoptinbox.openclose('open')">Subscribe now</button>

Is there a way that rather than having to click on the button, that it would automatically trigger when the page is opened? Therefore, eliminating having to click the button.

Thank You.

Yeah, sure. Give the button an ID and lose the inline event handler:

<button id="myButton">Subscribe now</button>

Then put this at the bottom of your page:

const button = document.querySelector('#myButton');
button.addEventListener('click', () => { ddoptinbox.openclose('open') });
button.click();

Thank you for your reply.

Hereā€™s the code at the bottom of the page and itā€™s not opening. I tried moving the button up to the top and it didnā€™t make any difference either.

<button id="myButton">Subscribe now</button>


<script>
const button = document.querySelector('#myButton');
button.addEventListener('click', () => { ddoptinbox.openclose('open') });
button.click();
</script>

Which browser are you testing in?
Have you got a link to this not working?

Sorry, I should have checked other browsers first. I am using the latest IE. It does work in FF and Chrome.

Can this not work in IE?

Oh ok. Maybe consider updating your browser?

IE compatible version:

<button id="myButton">Subscribe now</button>
<script>
  var button = document.querySelector('#myButton');
  button.addEventListener('click', function() { ddoptinbox.openclose('open') });
  button.click();
</script>
2 Likes

IE (even version 11) is incompatable with most of ES6. Microsoft will tell you to use Edge instead, because theyā€™ve mothballed IE as of 2016. Edge is mostly ES6 compliant (as are most browsersā€¦ dont think thereā€™s any browser that actually is 100% compliant at this point)

1 Like

Yes, thank you - that does work in all now. One last question, can there be a split second delay before the open code kicks in?

This is actually opening a modal that zooms in when the button is clicked. In IE with your code you get that zoomed in look. In FF and Chrome is just opens unless you close the modal and then click on the underlying button, then it zooms. So I was thinking that if there was a slight delay, that would give it the zoom effect.

Thanks

Dunno about browsers, but WebKit is 100% compliant

And with the ā€œExperimental JavaScript featuresā€ flag enabled, Chrome Canary scores 100% on the entirety of the Kangax table for ES6.

Use setTimeout.

setTimeout(function() { button.click(); }, 1000);

This relies on button being in global scope (which it is in the example).

1 Like

Interesting. According to the actual table, itā€™s failing a couple of incorrect variable names (U+102C0 and U+2E2F), but those are such edge cases iā€™m not surprised :wink: anyway, off topic. To the future, and arrow functions everywhere!

Indeed. ES6 all the things :slight_smile:

I must have this incorrect because I am not getting the delay even when set to 5000.

<script>

    setTimeout(function () { button.click(); }, 5000);

    var button = document.querySelector('#myButton');
    button.addEventListener('click', function () { ddoptinbox.openclose('open') });
    button.click();
    
</script>

Remove the second reference to button.click(); :slight_smile:

<script>
  var button = document.querySelector('#myButton');
  button.addEventListener('click', function () { ddoptinbox.openclose('open') });
  setTimeout(function () { button.click(); }, 5000);
</script>
2 Likes

Thank you for that - spot on!

I really appreciate your help and expertise. I am just an old dog trying to learn some new tricks! Envious of your vast knowledge.

Thanks again!

1 Like

No worries.

Iā€™m not exactly a spring chicken either :slight_smile:

1 Like

ā€¦But you are a smarter not so young spring chicken!

Talking about delays and zoom effects, is there js and/or css that will give you that effect like years ago IE only browsers were doing with transitions?

Iā€™m not sure which effect you are on about. There is definitely something called CSS transitions. Is that what you mean?

1 Like

Yes, thank you for that!

If you are trying to transition in your modal, there might be a built-in option for that.