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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need code fix - Include file within Javascript

    The following 'random content' script that is working great...

    Code:
    //RANDOM CONTENT
    function hometext() {
    var count = 3;
    var r = Math.ceil(Math.random() * count);
    if (r == 1) return "Mary had a Little Lamb";
    if (r == 2) return "Humpty Dumpty";
    if (r == 3) return "Little Jack Horner">";
    }
    
    // End -->
    The problem is that I want to make the text editable by the client.
    I'm using 'include' files throughout the site, so I would love to change it to something like this...

    Code:
    //RANDOM CONTENT
    function hometext() {
    var count = 3;
    var r = Math.ceil(Math.random() * count);
    if (r == 1) return "<!--#include virtual="include/hometext1.html" -->";
    if (r == 2) return "<!--#include virtual="include/hometext2.html" -->";
    if (r == 3) return "<!--#include virtual="include/hometext3.html" -->">";
    }
    
    // End -->
    This breaks the Javascript (I'm assuming because it has illegal characters in it).

    Is there a code change I could make that would make it work? Or are 'includes' not allowed here?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try to escape '"' as '\"':
    Code:
    //RANDOM CONTENT
    function hometext() {
    var count = 3;
    var r = Math.ceil(Math.random() * count);
    if (r == 1) return "<!--#include virtual=\"include/hometext1.html\" -->";
    if (r == 2) return "<!--#include virtual=\"include/hometext2.html\" -->";
    if (r == 3) return "<!--#include virtual=\"include/hometext3.html\" -->\">";
    }
    
    // End -->
    but I don't think it work with ssi

    regards

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for taking a look.

    * And for reminding me of the 'escape' for quotes - I'm still pretty new and forgot about that!

    I think I have an idea for another way to do this. Not quite as easy as this would have been, but it'll work.

    Best wishes.


  •  

    Posting Permissions

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