Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

onload function help

Featured Replies

Hello can anyone tell me why an onload function won't work where an onclick function will?

 

  
<p onload="getRequest( 'getOffers.php', updateTarget )">
Click Here. </p>
<div id="target"> </div>      
                      <br />

 

From here I wish to use ajax to automatically populate special offers every 20 seconds randomly switching through them.

 

 <p onclick="getRequest( 'getOffers.php', updateTarget )">

 

This is the working code I am using right now which randomly switches through special offers on user click. My goal is to remove the need of the user click, instead switch every 20 seconds.

 

A point in the right direction would be greatly appreciated.

Please reply if you need more code to help with this query.

  • Author

Ah good stuff. Any suggestions about the second part?

I read this on the internet somewhere... this any help at all?

 

function foo() {
// do stuff
// ...

// make a delayed recursive call to foo
setTimeout(foo, 3000);
}

I would highly recommend you take a look at jQuery. pretty sure you could implement this with it no problem

Some fantastic video tutorials can be found here

I would suggest you watch them all. They may take a while to learn initially but they will probably give you inspiration for more interactivity in the future as well as solving this problem

@epic - I'm pretty sure you only need to define a timeout once for it to repeat itself. I think that function might end up being called increasingly more with each 3 second gap (exponentially more actually). I could however be wrong on this

  • Author

I think I am responsible for the recent lack of fluffy white kittens then.

 

Thanks Jay but it is an assignment and the requirement is to use self written ajax to produce the desired result. I just have little idea of how to go about this.

 

I am actually looking at your website now at the timer class. Is this the sort of thing that could be adapted to meet my requirements?

  • Author

Right after a lot of trial and error I have my special offers automatically and randomly loading, however it switches every half second, I have tried to alter the code to affect the timer to no avail. I assume I need some sort of timer delay? maybe another function? any idea? Here's the javascript that affects the population:

function updateTarget( txt ) {    
             document.getElementById('target').innerHTML = txt; 
                            } 

function commence(updateTarget) { 	
            timer = setInterval("getRequest('getOffers.php', updateTarget);", 100);
                               } 	
var timer = 0; 	

  • Author

ahh trés bien. Can't thank you enough, gonna have to put you on my references. +1 for all your posts.

  • Author

Oh, final question. The 20 second switch works fine although the first special offer takes 20 seconds to appear is there any way I can get one to appear on load before the switcher starts?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.