Results 1 to 6 of 6

Thread: Running a function on another page

  1. #1
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Running a function on another page

    Hi guys, I hope you can point me in the right direction.

    I would like to have a link that runs a javascript command on another page on the site. The function would be to have a div that fades in color (to guide the user to where he/she is supposed to look at

    Below is the code that works if it's all on the same page.


    <html>
    <head>
    <script type="javascript">

    function colorFade(id,element,start,end,steps,speed) {
    var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
    var target = document.getElementById(id);
    steps = steps || 20;
    speed = speed || 20;
    clearInterval(target.timer);
    endrgb = colorConv(end);
    er = endrgb[0];
    eg = endrgb[1];
    eb = endrgb[2];
    if(!target.r) {
    startrgb = colorConv(start);
    r = startrgb[0];
    g = startrgb[1];
    b = startrgb[2];
    target.r = r;
    target.g = g;
    target.b = b;
    }
    rint = Math.round(Math.abs(target.r-er)/steps);
    gint = Math.round(Math.abs(target.g-eg)/steps);
    bint = Math.round(Math.abs(target.b-eb)/steps);
    if(rint == 0) { rint = 1 }
    if(gint == 0) { gint = 1 }
    if(bint == 0) { bint = 1 }
    target.step = 1;
    target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
    }

    // incrementally close the gap between the two colors //
    function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
    var target = document.getElementById(id);
    var color;
    if(target.step <= steps) {
    var r = target.r;
    var g = target.g;
    var b = target.b;
    if(r >= er) {
    r = r - rint;
    } else {
    r = parseInt(r) + parseInt(rint);
    }
    if(g >= eg) {
    g = g - gint;
    } else {
    g = parseInt(g) + parseInt(gint);
    }
    if(b >= eb) {
    b = b - bint;
    } else {
    b = parseInt(b) + parseInt(bint);
    }
    color = 'rgb(' + r + ',' + g + ',' + b + ')';
    if(element == 'background') {
    target.style.backgroundColor = color;
    } else if(element == 'border') {
    target.style.borderColor = color;
    } else {
    target.style.color = color;
    }
    target.r = r;
    target.g = g;
    target.b = b;
    target.step = target.step + 1;
    } else {
    clearInterval(target.timer);
    color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
    if(element == 'background') {
    target.style.backgroundColor = color;
    } else if(element == 'border') {
    target.style.borderColor = color;
    } else {
    target.style.color = color;
    }
    }
    }

    // convert the color to rgb from hex //
    function colorConv(color) {
    var rgb = [parseInt(color.substring(0,2),16),
    parseInt(color.substring(2,4),16),
    parseInt(color.substring(4,6),16)];
    return rgb;
    }
    </script>

    </head>
    <body>

    <a href="javascript:colorFade('fade1','background','D7FFCC','ffffff',100,70)">Click here to see a div fade</a>
    <br><br>

    <div id="fade1"> This div will fade</div>

    </body>
    </html>

    Can someone please tell me what I need to put on the other page? Is this an onLoad issue?

    Thank you for all the help!

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I'm not sure I completely understand your question.

    1. Do you simply want to apply this effect on other pages? Just copy the script to those pages (or better yet, put the script in its own external file and use it on any page you like).

    2. or, do you want page "A" to control what's happening on page "B"? This is not possible using standard javascript. You could use web sockets or cross-document messaging (HTML5 APIs), but these are not yet fully/widely supported.

    please elaborate.

  3. #3
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I'd like the final product to have a link on page 'A', and when you click on that link, a new page 'B' opens, and the javascript automatically runs on a div on page 'B'.

    Thank you, let me know if I'm still being too vague :/


    Edit:

    Here is more of what I'm referring to. The second example, where you have to click on a link for the javascript to run, but I would like the div on another page.
    Last edited by RoRiddler; 01-16-2012 at 04:55 PM.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    would you want the javascript to run every time page "B" opens?
    -- put the script on page "B" and run it onload.

    or, only when the user opens page "B" by using the link on page "A"?
    -- have page "A" pass some kind of token to page "B" (e.g., in the query string). run the script when you find the token.

  5. #5
    Join Date
    Dec 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much!

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

Bookmarks

Posting Permissions

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