My button uses the following. It produces a random post within my blog whenever a user clicks it.

<div id="myLuckyPost" class="random button"></div><script type="text/javascript"> function showLucky(root){ var feed = root.feed; var entries = feed.entry || []; var entry = feed.entry[0]; for (var j = 0; j < entry.link.length; ++j){if (entry.link[j].rel == 'alternate'){window.location = entry.link[j].href;}}} function fetchLuck(luck){ script = document.createElement('script'); script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky'; script.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(script); } function feelingLucky(root){ var feed = root.feed; var total = parseInt(feed.openSearch$totalResults.$t,10); var luckyNumber = Math.floor(Math.random()*total);luckyNumber++; a = document.createElement('a'); a.href = '#random'; a.rel = luckyNumber; a.onclick = function(){fetchLuck(this.rel);}; a.innerHTML = '<center><span id="icon-random"></span></center><span class="visible-lg">Random</span>'; document.getElementById('myLuckyPost').appendChild(a); } </script> <script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script>

I'd like for this button to automatically be clicked whenever someone enters through my homepage so my visitors can get right into the thick of things. So far this is what I've found:

<script>
window.setTimeout('clickit()',5000);
function clickit(){
   location.href = document.getElementById("autoid");
}
</script>

I'm just not sure where to put the ID, and what it should be. I've tried a few things to no avail. Thanks a ton to anyone who is able to help.

Rather than trying to mimic the click of a button, why not load the function using window.onload? The following example executes the foo fuction when the onload event is triggerd.

<script>
function foo() {
  document.write("Hello World");
}

window.onload=function() {
   foo()
};

</script>

You can resuse the same foo function to execute when the button is clicked.

I get what you're saying, but how would I manipulate that script to make it onloadable instead of clickable. I didn't write the script, in the first place... so I'm not exactly the master of it :-)

Do I just change the onclick to onload? Is it that simple? Thank you for the assistance.

<script type="text/javascript"> function showLucky(root){ var feed = root.feed; var entries = feed.entry || []; var entry = feed.entry[0]; for (var j = 0; j < entry.link.length; ++j){if (entry.link[j].rel == 'alternate'){window.location = entry.link[j].href;}}} function fetchLuck(luck){ script = document.createElement('script'); script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky'; script.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(script); } function feelingLucky(root){ var feed = root.feed; var total = parseInt(feed.openSearch$totalResults.$t,10); var luckyNumber = Math.floor(Math.random()*total);luckyNumber++; a = document.createElement('a'); a.href = '#random'; a.rel = luckyNumber; a.onload = function(){fetchLuck(this.rel);}; a.innerHTML = '<center><span id="icon-random"></span></center><span class="visible-lg">Random</span>'; document.getElementById('myLuckyPost').appendChild(a); } </script> <script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script>

Ok so I'm not familiar with this script, but to make it more readable, i expanded it. Without a working model since I dont have access to the feed, i would try this...

<div id="myLuckyPost" class="random button"></div>

<script type="text/javascript"> 
function showLucky(root){ 
 var feed = root.feed; 
 var entries = feed.entry || []; 
 var entry = feed.entry[0]; 
 for (var j = 0; j < entry.link.length; ++j){
  if (entry.link[j].rel == 'alternate'){
        window.location = entry.link[j].href;
  }
 }
} 

function fetchLuck(luck){ 
 script = document.createElement('script'); 
 script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
 script.type = 'text/javascript'; 
 document.getElementsByTagName('head')[0].appendChild(script); 
} 

function feelingLucky(root){ 
 var feed = root.feed; 
 var total = parseInt(feed.openSearch$totalResults.$t,10); 
 var luckyNumber = Math.floor(Math.random()*total);
 luckyNumber++; 
 a = document.createElement('a'); 
 a.href = '#random'; 
 a.rel = luckyNumber; 
 a.onclick = function(){
   fetchLuck(this.rel);
 }; 
 a.innerHTML = '<center><span id="icon-random"></span></center><span class="visible-lg">Random</span>';
 document.getElementById('myLuckyPost').appendChild(a); 
} 

window.onload=function() {
   fetchLuck(Math.floor(Math.random()*parseInt(feed.openSearch$totalResults.$t,10)));
};

</script> 

<script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script> 

Notice where I placed the window.onload function. I am calling the fetchLuck() method just like the click event does on the <a> element you are creating. The click event is passing a random number. I just copied that code that generates the random number. It would be better if you move that random number generator to its own function so its easier to read if you get this working...

Couldn't get that to work, but I'll tinker with what you did for a while. There's gotta be something in there to teach me something. I truly appreciate your efforts.

My site is http://www.thestormybrain.com/ if seeing the entirety of the script and functionality helps ni the slightest. The green button listed as 'Random' is what I'm attempting to trigger. Doesn't seem like it should be too difficult. I had a Website Tour script one time that could click any link then go from page to page.

I'm headed back to the drawing board. Thanks again!

yeah...looked at the dev console in my browser and this..

window.onload=function() {
   fetchLuck(Math.floor(Math.random()*parseInt(feed.openSearch$totalResults.$t,10)));
};

isnt going to work because feed isnt initialized at that point.

also..

I did some reading and testing and came across this link:
http://blog.stchur.com/2010/01/15/programmatically-clicking-a-link-in-javascript/

seems interesting. Not sure exactly if you can incorporate this with your issue, but it seems to work with regard to automatically similuating the clikcing of a link.

I'll check it out. Thanks. I kinda feel smarter knowing it's not so easy to solve, but it's still a bit frustrating :-)

Cheers!

You should try adding the "bold" part at "this" point of your code:
... document.getElementById('myLuckyPost').appendChild(a); a.click(); } </script> ...

Thanks, Troy. I tried that, however, and it doesn't seem to do the trick. I'm trying to get the link to click automatically during the onLoad process. It seems easy enough with simple links, but the link I'm working with produces a random post from all of my posts on blogspot. If you have any other ideas, I'd love to hear em.

I really appreciate your time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.