Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-25-2008, 06:12 AM   PM User | #1
ddemaree
New to the CF scene

 
Join Date: Apr 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
ddemaree is an unknown quantity at this point
simple script request

i posted earlier about a project i'm working on and i'd like to give a thanks to mjlorbet of these forums for his help but i've just got impatient. i just need a script that will load a random page in a selected frame using a text file for url's
if someone could write up a script where the only thing i have to do is replace a couple values (like the web address) id really appreciate it
here some details to help you out

frames will be named master and slave. master being the main frame where the js code will be and slave being the target for the redirect url

redirect url will be "http://homepage.com/profile/?id=XXXXXX" with XXXXXX being random values from a text file which will be named id.txt (always 6 digits)

no redirect delay

select XXXXXX from a line number in id.txt file

example:

254698
257961
349857
273549

if the script selected line 4 the url would be "http://homepage.com/profile/?id=349857"

i know this is alot to ask but if theres anyone whos good at javascript and has a few minutes i'd appreciate the help
ddemaree is offline   Reply With Quote
Old 04-25-2008, 09:13 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
why a text file and not a js file?

Anyway, how do you do to inport/load your txt file into the HTML document? Under which aspect (a string?)?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-25-2008, 04:48 PM   PM User | #3
ddemaree
New to the CF scene

 
Join Date: Apr 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
ddemaree is an unknown quantity at this point
Quote:
Originally Posted by Kor View Post
why a text file and not a js file?

Anyway, how do you do to inport/load your txt file into the HTML document? Under which aspect (a string?)?
i could use a js files too, basically i just dont want to have to use sql or any other databases. the exact way the id's will be recorded is

584967%20
267954%20
597682%20
329765%20
875942%20

and so forth. if your wondering i use the %20 as a separater so my php file searches for the id properly because i was having problems before with duplicate entries. i can use anything as a separater but i found if i use %20 if it accidently runs through the entire line and includes it in the url it still loads the page properly (in IE and FF anyway)

i guess you'd consider that strings, they aren't variables so i think that only leaves strings. i just dont want to have to manually update a js file everytime a name is added. if it would be easier the write directly to the js file instead of reading an external file i can do that too as long it still didnt allow duplicates. heres the snippet from my php file that checks the user variable for existence then writes it.

PHP Code:
<?php


function registerUser($user){
    
$errorText '';
    if (
strlen($user) != 6$errorText "Id must be six digits long.";

    
    
    
// Check user existance    
    
$pfile fopen("id.txt","a+");
    
rewind($pfile);

    while (!
feof($pfile)) {
        
$line fgets($pfile);
        
$tmp explode("%20"$line);
        if (
$tmp[0] == $user) {
            
$errorText "That user id is already registered!";

                        break;
        }
    }
    
    
// If everything is OK -> store user data
    
if ($errorText == ''){
        
        
        
fwrite($pfile"\r\n$user%20");
    }
    
    
fclose($pfile);
    
    
    return 
$errorText;
}


?>
ddemaree is offline   Reply With Quote
Old 04-25-2008, 06:11 PM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
I would have used AJAX like that: send the id input to the php application, and wait for a response (echo). If the response is not an error text, give, via javascript, a new location href to the document.

Do you know how to write an AJAX request?
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-25-2008, 06:25 PM   PM User | #5
ddemaree
New to the CF scene

 
Join Date: Apr 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
ddemaree is an unknown quantity at this point
Quote:
Originally Posted by Kor View Post
I would have used AJAX like that: send the id input to the php application, and wait for a response (echo). If the response is not an error text, give, via javascript, a new location href to the document.

Do you know how to write an AJAX request?
i'm not familiar with ajax, i think it has to do with js and xml but im not sure. i could research it a little though.

so whats happening is the user input goes to php then gets passes on to ajax to be written. or does it goto ajax then php then ajax again. or just ajax then php? or am i completely off?
ddemaree is offline   Reply With Quote
Old 04-25-2008, 07:00 PM   PM User | #6
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
AJAX is a sum of techniques. It is based on a special object (ActiveXObject() for IE5, IE6, XMLHttpRequest() for IE7 and the rest of the browsers), javascript and (not required) XML.

That special object does a request (same as a classical submit) except that the request is made asynchronously (without changing the session) and receives an answer which is handled with javascript.

How could work AJAX in your case?

AJAX request -> php file ->txt file ->response from txt ->response from php-> javascript handle and display on page

As I said, works like a submit, except that data is handled "behind the scene", without changing the session.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 04-25-2008 at 07:02 PM..
Kor is offline   Reply With Quote
Old 04-25-2008, 07:04 PM   PM User | #7
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
already suggested that in the other thread, but glad to see i'm remembered
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Old 04-25-2008, 07:08 PM   PM User | #8
ddemaree
New to the CF scene

 
Join Date: Apr 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
ddemaree is an unknown quantity at this point
ok, i think i got that. so do i need to write a php script that returns all the values from the txt to the js file to be randomly selected and redirected or should the php file randomly pick a line from the txt file to be returned to js and redirected. is that what you saying?
ddemaree is offline   Reply With Quote
Old 04-25-2008, 07:23 PM   PM User | #9
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
basically all your php file has to do is output your file from the harddrive exactly as is, no xml needed

your ajax script will be loaded in the context of the window requesting the file, so the responseText property of the ajax object (or xhr, short for xml http request) would contain the contents of that file (once it has been received from the server).

you would then use
Code:
 
window["links"] = this.responseText.split("\n");
in the onreadystatechange handler of the xhr object (checking to make certain the readyState is 4 and status is either ok, cached, or ok from local (200, 304, 0 respectively))

after that you would call a function to select the random result from the list, comparing it to the list of selected indicies
Code:
 
var prevSelected = new Array();
function checkPrev(index){
var found = false;
for(var a = 0; a < prevSelected.length; a++)
    found = found || (prevSelected[a] == index);
return found;
}
which would be called from your randomizing function to make sure the same index hasn't been selected twice.
Code:
 
var selectionTimeout;
function getRandom(){
    var tmp = -1;
    if(links.length > prevSelected.length){
        var trand = parseInt(Math.random()*links.length);
        while(checkPrev(trand))
            trand = parseInt(Math.random()*links.length);
        tmp = trand;
    }
    return tmp;
}
function showProfile(){
var useIndex = getRandom();
//your code here
}
selectionTimeout = setTimeout(showProfile, 5000);
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:42 AM.


Advertisement
Log in to turn off these ads.