Skip to main content
Inspiring
May 6, 2006
Answered

For Javascript Guru's

I've searched online, but can't find a similiar example.

I want the site owner to be able to edit a certain part of script, so I have put a section in a .cfm file so it can be edited by the site owner.
In the <script type=> tag I have put a <cfinclude> to the file, & then listed the rest of the script.
It doesn't appear to read the cfinclude file, because if I paste that section of script directly into the rest, the script works online.

Can I include a file inside javascript?
    This topic is closed to new replies. Start a new post to keep the conversation going.
    539ECorrect answer
    Inspiring
    May 6, 2006
    If view source, yes it's there.
    Except, it is displaying the include file as one long line starting with the comment.

    The actual include file has the returns as shown above.

    I know what it is now.
    When the owner edits it (with fckeditor) the returns are lost.
    It saves it as one long line.

    So now I just need to figure out how to keep returns with fckeditor.

    Thanks for trying to help.

    5 replies

    Inspiring
    May 6, 2006
    This shoud work, can you please post your code?
    Translate
    539EAuthor
    Inspiring
    May 6, 2006
    <script type="text/javascript">
    /*************************************************************************
    This code is from Dynamic Web Coding at http://www.dyn-web.com/
    See Terms of Use at http://www.dyn-web.com/bus/terms.html
    regarding conditions under which you may use this code.
    This notice must be retained in the code as is!
    *************************************************************************/

    function getDocHeight(doc) {
    var docHt = 0, sh, oh;
    if (doc.height) docHt = doc.height;
    else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
    }
    return docHt;
    }

    function setIframeHeight(iframeName) {
    var iframeWin = window.frames[iframeName];
    var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
    if ( iframeEl && iframeWin ) {
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous
    var docHt = getDocHeight(iframeWin.document);
    // need to add to height to be sure it will all show
    if (docHt) iframeEl.style.height = docHt + 30 + "px";
    }
    }

    function loadIframe(iframeName, url) {
    if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;
    return false;
    }
    else return true;
    }

    <cfinclude template="menuDrop2.cfm">

    var menuwidth='165px' //default menu width
    var menubgcolor='#cococo' //menu bgcolor
    var menubgimage='images/linkbck2.gif' //menu bgimage
    var disappeardelay=250 //menu disappear speed onMouseout (in miliseconds)
    var hidemenu_onclick="yes" //hide menu when user clicks within menu?
    var ie4=document.all
    var ns6=document.getElementById&&!document.all

    if (ie4||ns6)
    document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+';background-image:url('+menubgimage+');" onMouseover="clearhidemenu();" onMouseout="dynamichide(event);"></div>')

    function getposOffset(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
    totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
    parentEl=parentEl.offsetParent;
    }
    return totaloffset;
    }

    function showhide(obj, e, visible, hidden, menuwidth){
    if (ie4||ns6)
    dropmenuobj.style.left=dropmenuobj.style.top="-500px"
    if (menuwidth!=""){
    dropmenuobj.widthobj=dropmenuobj.style
    dropmenuobj.widthobj.width=menuwidth
    }
    if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
    obj.visibility=visible
    else if (e.type=="click")
    obj.visibility=hidden
    }

    function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
    }

    function clearbrowseredge(obj, whichedge){
    var edgeoffset=0
    if (whichedge=="rightedge"){
    var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
    dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
    if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
    edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
    }
    else{
    var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
    var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
    dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
    if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure){ //move up?
    edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
    if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
    edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
    }
    }
    return edgeoffset
    }

    function populatemenu(what){
    if (ie4||ns6)
    dropmenuobj.innerHTML=what.join("")
    }


    function dropdownmenu(obj, e, menucontents, menuwidth){
    if (window.event) event.cancelBubble=true
    else if (e.stopPropagation) e.stopPropagation()
    clearhidemenu()
    dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
    populatemenu(menucontents)

    if (ie4||ns6){
    showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)

    dropmenuobj.x=getposOffset(obj, "left")
    dropmenuobj.y=getposOffset(obj, "top")
    dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
    dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
    }

    return clickreturnvalue()
    }

    function clickreturnvalue(){
    if (ie4||ns6) return false
    else return true
    }

    function contains_ns6(a, b) {
    while (b.parentNode)
    if ((b = b.parentNode) == a)
    return true;
    return false;
    }

    function dynamichide(e){
    if (ie4&&!dropmenuobj.contains(e.toElement))
    delayhidemenu()
    else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
    delayhidemenu()
    }

    function hidemenu(e){
    if (typeof dropmenuobj!="undefined"){
    if (ie4||ns6)
    dropmenuobj.style.visibility="hidden"
    }
    }

    function delayhidemenu(){
    if (ie4||ns6)
    delayhide=setTimeout("hidemenu()",disappeardelay)
    }

    function clearhidemenu(){
    if (typeof delayhide!="undefined")
    clearTimeout(delayhide)
    }

    if (hidemenu_onclick=="yes")
    document.onclick=hidemenu
    </script>

    ====== menuDrop2.cfm =======
    //Contents for News
    var News=new Array()
    News[0]='<a href="content.cfm?pg=issue">Latest Issue</a>'
    News[1]='<a href="content.cfm?pg=contest">Contest</a>'
    News[2]='<a href="content.cfm?pg=subscribe">Subscribe</a>'
    News[3]='<a href="content.cfm?pg=Unsubscribe">Un-Subscribe</a>'
    News[4]='<a href="content.cfm?pg=archives">Archives</a>'


    //Contents for Articles
    var Art=new Array()
    Art[0]='<a href="mind.cfm">Mind</a>'
    Art[1]='<a href="body.cfm">Body</a>'
    Art[2]='<a href="spirit.cfm">Spirit</a>'
    Art[3]='<a href="wealth.cfm">Wealth</a>'
    Art[4]='<a href="health.cfm">Health</a>'
    Art[5]='<a href="content.cfm?pg=books">Books</a>'
    Art[6]='<a href="content.cfm?pg=business">Business</a>'
    Art[7]='<a href="content.cfm?pg=spotlight">Business Spotlight</a>'

    ============
    Then, a menu image as so....
    <a href="" onClick="return clickreturnvalue();" onMouseover="dropdownmenu(this, event, News, '150px')" onMouseout="delayhidemenu()"> <img src="images/news.gif"></a>

    Then drop down menu doesn't show with the include.
    Translate
    Inspiring
    May 6, 2006
    That should work, is it throwing an error or just not working how you expected?

    If you view source is it there?
    Translate