Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Quote Rotation Without Page Reload.

    I just have a question how to use ajax or any javascript with this. I have quotes rotating at page reload but am wondering how to have them change withour reloading.

    <code>
    <?php

    $quoteFile = file("quotes.txt");
    shuffle($quoteFile);

    echo "$quoteFile[0]<br />\n";

    ?>
    </code>

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,190
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Code:
    <script>
    //support code 
    function IO(U, V) {
    //LA MOD String Version. A tiny ajax library. ny DanDavis
        var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
        X.open(V ? 'PUT' : 'GET', U, !1);
        X.setRequestHeader('Content-Type', 'text/html')
        X.send(V ? V : '');
    return X.responseText;}
    
    function Rnd(w) {return parsetInt(Math.random() * (w + 1));}
    Array.prototype.random = function () {return this[Rnd(this.length - 1)];}
    
    
    
    //now custom code
    
    var allQuoes = IO("quotes.txt").split(/\r?\n/g);
    document.getElementsByTagName("code")[0].innerHTML = allQuotes.random();
    </script>
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    New Coder
    Join Date
    Jun 2008
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I get an error on this line.

    document.getElementsByTagName("code")[0].innerHTML = allQuotes.random();

    it says its null or not an object ?

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,190
    Thanks
    10
    Thanked 569 Times in 550 Posts
    i assumed from the code you posted that you were using a <code> tag to display the quote.

    if you dont, put some tag in your document where you want the quotes to land, and give it an id attrib; for instance:

    <div id="output">Loading Quote...</div>


    then replace the bad line in my first post with :

    Code:
    document.getElementById("output").innerHTML = allQuotes.random();
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #5
    New Coder
    Join Date
    Jun 2008
    Location
    127.0.0.1
    Posts
    43
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Uh, like this?

  • #6
    New Coder
    Join Date
    Jun 2008
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ok I have it like this and now my error is expecting an object.

    <script>
    //support code
    function IO(U, V) {
    //LA MOD String Version. A tiny ajax library. ny DanDavis
    var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
    X.open(V ? 'PUT' : 'GET', U, !1);
    X.setRequestHeader('Content-Type', 'text/html')
    X.send(V ? V : '');
    return X.responseText;}

    function Rnd(w) {return parsetInt(Math.random() * (w + 1));}
    Array.prototype.random = function () {return this[Rnd(this.length - 1)];}



    //now custom code

    var allQuotes = IO("quotes.txt").split(/\r?\n/g);
    document.getElementsByTagName("output").innerHTML = allQuotes.random();
    </script>
    <div id="output"><?php

    $quoteFile = file("quotes.txt");
    shuffle($quoteFile);

    echo "$quoteFile[0]<br />\n";

    ?>
    </div>

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,190
    Thanks
    10
    Thanked 569 Times in 550 Posts
    document.getElementById("output").innerHTML = allQuotes.random();
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #8
    New Coder
    Join Date
    Jun 2008
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    document.getElementById("output").innerHTML = allQuotes.random();

    is null or not an object error

  • #9
    New Coder
    Join Date
    Jun 2008
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    it might be due to me using the same

    getElementById

    in another script which is my banner rotation sript that is probably what's throwing it off is there anyway I can have both run at the same time?

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,190
    Thanks
    10
    Thanked 569 Times in 550 Posts
    hmm no ideas. yes, you can run several scripts at once.

    repost the whole page as you have it now and i can take a look at it...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #11
    New Coder
    Join Date
    Jun 2008
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I of course have banner ads filled in on my own site just for this post I have stripped them to display my code hopefully this helps shed light on the issue at hand. thanks.
    this is the right margin of the site. the other functions which are called up o in the rest of the site are functioning properly.
    Code:
    <script type="text/javascript">
          
    adds=[ // url,alt,image
    
    ["url","alt","image"],
    
    ["url","alt","image"],
    
    
    ["url","alt","image"]
    
    ]
    
    count=0
    count = Math.floor(Math.random()*adds.length);
    onload=function test(){
    MaloriaN.start();
    test2();
    
    oLink=document.getElementById("mylink")
    oImg=document.getElementById("myimg")
    
    
    
    oLink.href=adds[count][0]
    oImg.alt=adds[count][1]
    oImg.src=adds[count][2]
    
    count++
    if(count==adds.length){
    count=0
    }
    
    setTimeout("test()",48000)
    }
    
    
    </script>
    <a id="mylink" href="#null" target="_blank"><img id="myimg" src="" alt="" style="width:125px;height:125px" border="0"><div id="mytxt"></div></a>
    
    <b><font size="2" color="darkblue"><tr><td colspan=2 align=center><hr color="#F6EED0" noshade SIZE="3" width="130"><strong><b><font size="3" color="#F6EED0"><center><img src="main/feedback.gif" width="100" height="15" border="0"></center></font></b></strong><b><font size="2" color="darkblue"><hr color="#F6EED0" noshade SIZE="3" width="130">
    <i>
    <?php
    
    $quoteFile = file("quotes.txt");
    shuffle($quoteFile);
    
    echo "$quoteFile[0]<br />\n";
    
    ?> 
    </i><br><a href=quote_add.php onClick="NewWindow(this.href,'client_add','450','480','no');return false" onMouseOver="window.status='Leave your Feedback'; return true" onMouseOut="window.status=''; return true" target=_blank><font size=2 color="navy">- Send Us Feedback -</font></a><br>
    <FORM enctype="text/plain" onSubmit="return sendMail()"  METHOD="POST" ACTION="mailto:webmaster@webmaster.com?subject=Feedback">
    <TABLE WIDTH="50&#37;" BORDER="0" CELLSPACING="0" 
      CELLPADDING="0" ALIGN="CENTER">
    <div align="center"><br>
    <script language="javascript">
    // Author: Eric King Url: http://redrival.com/eak/index.shtml
    // This script is free to use as long as this info is left in
    // Courtesy of SimplytheBest.net (http://simplythebest.net/info/dhtml_scripts.html)
    var win = null;
    function NewWindow(mypage,myname,w,h,scroll){
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
    settings =
    'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=no'
    win = window.open(mypage,myname,settings)
    }
    </script><br>
    Last edited by Trinithis; 06-20-2008 at 06:54 AM.

  • #12
    New Coder
    Join Date
    Jun 2008
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts
    any ideas as to why the feedback still gives me errors with the current coding


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •