/    Sign up×
Community /Pin to ProfileBookmark

receiving javascript error with random iframe script

Hello all, and thank-you for reading my post!

Am new to javascript and am in need of a little help.

What I am attempting to do is this:

I have an iframe on a page – I’ve set the iframe up so that it randomly chooses between 10 different html pages, then displays that page.

Here is the script I am using:

<script language=”JavaScript”>
var ddd = new Array()
ddd[1] = ‘/page/a/001/index.html’
ddd[2] = ‘/page/a/002/index.html’
ddd[3] = ‘/page/a/003/index.html’
ddd[4] = ‘/page/a/004/index.html’
ddd[5] = ‘/page/a/005/index.html’
ddd[6] = ‘/page/a/006/index.html’
ddd[7] = ‘/page/a/007/index.html’
ddd[8] = ‘/page/a/008/index.html’
ddd[9] = ‘/page/a/009/index.html’
ddd[10] = ‘/page/a/010/index.html’
var xxx = Math.random()
var i = Math.round(xxx*9)+1
document.write(‘<iframe src=’+ddd[i]+’ width=300 height=597 frameborder=0 scrolling=no></iframe>’)
</script>

This script actually does work, and exactly the way it’s suppose to – HOWEVER, I am getting a little ‘javascript syntax error exclamation point’ at the bottom of the screen.

Any assistance in locating and correcting the ‘error’ would be greatly appreciated!!!

Thank-you!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@felgallJul 02.2006 — One thing is that with the code the way you have it the [1] and [10] entries will be selected only half as often as the other 8 as they are sharing 1/9th of the random results between them. That isn't causing an error though.
Copy linkTweet thisAlerts:
@JustinUpperCaseauthorJul 02.2006 — felgall...

Yes, I have noticed that pages 1 and 10 are not chosen as often as the others, but that's ok, I'll compensate for this later by adding a few extra lines for 1 and 10.

Thanx for your reply!
Copy linkTweet thisAlerts:
@konithomimoJul 02.2006 — there are no errors in IE . . . meaing no yellow warning sign . . .
Copy linkTweet thisAlerts:
@JustinUpperCaseauthorJul 02.2006 — konithomino...

I am sorry to have to disagree with you, but I'm using IE on a PC and I'm looking at the 'javascript error sign' right now...

thanx for your thoughts though... ?
Copy linkTweet thisAlerts:
@JMRKERJul 02.2006 — I don't understand the reason behind the statement of felgall.

One thing is that with the code the way you have it the [1] and [10] entries will be selected only half as often as the other 8 as they are sharing 1/9th of the random results between them.
[/QUOTE]

Can you elaborate please? ?
Copy linkTweet thisAlerts:
@konithomimoJul 02.2006 — I used it in IE 6.0 on a PC running XP Pro, as well as two other computers,and there was no error with that script, so something else on your page is causing the script to crash, not the script itself.

There are a few things that you can do to the script though:

  • 1. Use type="text/javascript".


  • 2. Arrays start at 0, so start yours at 0.


  • 3 . Use Math.floor instead of Math.round

    &lt;script type="text/javascript"&gt;
    var ddd = new Array();
    ddd[0] = '/page/a/001/index.html';
    ddd[1] = '/page/a/002/index.html';
    ddd[2] = '/page/a/003/index.html';
    ddd[3] = '/page/a/004/index.html';
    ddd[4] = '/page/a/005/index.html';
    ddd[5] = '/page/a/006/index.html';
    ddd[6] = '/page/a/007/index.html';
    ddd[7] = '/page/a/008/index.html';
    ddd[8] = '/page/a/009/index.html';
    ddd[9] = '/page/a/010/index.html';
    var i = Math.floor(Math.random()*10);
    document.write('&lt;iframe src='+ddd[i]+' width=300 height=597 frameborder=0 scrolling=no&gt;&lt;/iframe&gt;')
    &lt;/script&gt;


    I don't understand the reason behind the statement of felgall.

    Can you elaborate please? ?[/QUOTE]


    the random function gives a decimal from 0 to slightly less than 1. The way that the OP had it (using Math.round) there probability of getting a 1 or 9 was much less, since round goes up or down . . . thus the reason that I used Math.floor which only goes down . . . meaning the same probability for all numbers (not exactly since all algorithms give more of the same answers )
    Copy linkTweet thisAlerts:
    @JustinUpperCaseauthorJul 02.2006 — konithomimo...

    You were right, and I was wrong.

    It wasn't the script that was causing the 'error'.

    After I read your post, I whittled away at the rest of my page until I finally found the cause - turns out it was an unacceptable file name (which has since been corrected).

    I'd like to thank you for your time, your insight and the editing of my script - my array now starts at 0 (zero)!

    I tried using .floor instead of .round as you suggested, but found that the script would return every number except ddd[9] = '010'; - I compensated for this by changing it back to .round and adding two extra lines for page's 1 and 10 (see modified script). This may be a bit clumsy but the script now returns page's 1 and 10 much more frequently.

    Here is my new 'working' (no error) script!


    <script type = "text/javascript" language = "Javascript">

    <!--

    var ddd = new Array();

    ddd[0] = '001';

    ddd[1] = '002';

    ddd[2] = '003';

    ddd[3] = '004';

    ddd[4] = '005';

    ddd[5] = '006';

    ddd[6] = '007';

    ddd[7] = '008';

    ddd[8] = '009';

    ddd[9] = '010';

    ddd[10] = '001';

    ddd[11] = '010';

    var i = Math.round(Math.random()*11);

    document.write('<iframe src="/page/a/'+ddd[i]+'/index.html" width=300 height=597 frameborder=0 scrolling=no></iframe>')

    //-->

    </script>



    Thanks again konithomimo!



    and thank-you to everyone who replied to my post!
    ×

    Success!

    Help @JustinUpperCase spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...