• Help with JavaScript building a website
    8 replies, posted
Hello, I'm quite new to javascript and I was wondering how would I go about doing a certain difficult task. I am trying to make a script that will grab another javascript file at random. I have 13 files with different sets of words to display. I need a javascript that will randomly grab one of these files every time I refresh the page. I have a feeling I need to use a math.random() code along with some sort of array 1-13. But I am so stumped. So far I have: [code] new var testimonial; window.onLoad = testGrab new array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); function testGrab() { math.random(array); } [/code] I am a Javascript n00b so if somebody could tell me why it isnt working I would much appreciate it! Also I can't figure out how to call upon a file.
you can't pass anything into math.random. It can only run as math.random(), which spits out a random decimal between 0 and 1. You're code doesn't seem to be written all that properly, and you aren't ever passing in your array. You could make it work like this: [cpp]function testGrab() { return (math.random() * 13); } var testimonial; window.onLoad = testGrab; var choice = testGrab(); [/cpp] [editline]05:46PM[/editline] wait, you don't need it to run onLoad... try something like this: [cpp]function testGrab() { var testimonial = Math.floor(Math.random() * 13 + 1); } window.onLoad = testGrab();[/cpp] assuming you want to choose a random testimonial 1-13 as the page loads. [editline]05:52PM[/editline] Javascript has restrictions when it comes to working with files for security reasons. You can import an XML file and parse it out to an object, there are guides to that, but the XML file must be contained in the same folder as the javascript file.
Well that code seems to do the trick, but it does defeat the purpose because of the file restriction! Thank you for the help though! I'll keep tweaking it and see if I can get this script to work. [editline]07:03PM[/editline] Actually I figured out all I need to do is link using [code]document.write("<script type='text/javascript' src='testimonials/testimonial' + testGrab + '.js></script>'") [/code] Thank you for the math script though, that tied this whole thing together!
[QUOTE=robmaister12;24908764][cpp]window.onLoad = testGrab();[/cpp][/quote] You want: [cpp]window.onload = testGrab;[/cpp] [quote] Javascript has restrictions when it comes to working with files for security reasons. You can import an XML file and parse it out to an object, there are guides to that, but the XML file must be contained in the same folder as the javascript file.[/QUOTE] What? No, it can grab any file on the same domain with XMLHttpRequest.
My web design is rusty, I haven't touched Javascript for about a year. Back when I did, I was using XMLDocument to grab XML files and parse them. And actually it wasn't restricted to the folder, just the domain... Sorry about that.
javascript is a web design code, not programming. silly user, you can't post in here, you can't even code, what are you doing here?
[QUOTE=Agent Texas;24922317]javascript is a web design code, not programming. silly user, you can't post in here, you can't even code, what are you doing here?[/QUOTE] Javascript is scripting. Scripting is programming.
But it's WEB scripting. KILL THE HEATHEN
[QUOTE=Agent Texas;24922317]javascript is a web design code, not programming. silly user, you can't post in here, you can't even code, what are you doing here?[/QUOTE] Being used in Web browsers doesn't magically disqualify JavaScript from being called a programming language. It's [url=http://en.wikipedia.org/wiki/Turing_completeness]Turing-complete[/url]; it can be used to do arbitrarily complex things. Have you looked at any [url=http://www.sencha.com/products/js/]ExtJS[/url] apps lately?
Sorry, you need to Log In to post a reply to this thread.