EE Converts and Breaks Javascript Code / Characters
Posted: 15 May 2008 05:51 PM   [ Ignore ]  
Summer Student
Total Posts:  9
Joined  04-01-2008

When I post an entry, EE is converting square brackets [..] characters to angle brackets <..>, which of course breaks the Javascript.  When I can save the entry and reopen it, the [] are still there; however, when it viewed the source of the served page in a browser, it is <>

Here is the code where it happens

function loadobjs(){
    
if (!document.getElementById)
        return
    for (
i=0; i<arguments.length; i++){
        
var file=arguments[i];
        var
fileref="";
        if (
loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
            
if (file.indexOf(".js")!=-1){ //If object is a js file
                
fileref=document.createElement('script')
                
fileref.setAttribute("type","text/javascript");
                
fileref.setAttribute("src", file);
        
}
            
else if (file.indexOf(".css")!=-1){ //If object is a css file
                
fileref=document.createElement("link")
                
fileref.setAttribute("rel", "stylesheet");
                
fileref.setAttribute("type", "text/css");
                
fileref.setAttribute("href", file);
            
}
        }
        
if (fileref!=""){
            document
.getElementsByTagName("head").item(0).appendChild(fileref)
            
loadedobjects+=file+" " //Remember this object as being already added to page
        
}
    }
}

The fifth line down

var file=arguments[i];


becomes

var file=arguments<i>;

I found a work-around by changing the code to

var file=arguments[i+0];

Note that there are other [] in code that are not getting converted

function swap(_name, _src) { document.images[_name].src = _src; }

Thanks

Profile
 
 
Posted: 15 May 2008 07:08 PM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  12433
Joined  04-29-2002

I can tell you why it’s happening.. [ i ] is the same as < i > - italics.

Hold on a sec and I’ll see if there is a workaround.

Are you posting this in as a code block, or what exactly?

 Signature 

Quick Reference - EE Trial Options - EE Wiki - Docs for updating a build

Profile
MSG
 
 
Posted: 16 May 2008 11:44 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  9
Joined  04-01-2008

The code appears inside of a script tag in an HTML file.

Profile
 
 
Posted: 16 May 2008 11:50 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15489
Joined  05-15-2004

Didn’t you say “When I post an entry…”? So I suppose this is part of an EE entry, right? EE will parse pmCode and replace it with HTML tags. The same would happen with

[b] [strong] [em]

and a few others, but only [ i] is likely to be used in a programming language.

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.

Profile
MSG
 
 
Posted: 16 May 2008 12:13 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  9
Joined  04-01-2008

Sorry for the confusion.  It is HTML part of an entry in EE, not a static HTML page.

Is there a way to disable conversion?  I suppose you could use a different variable in the javascript.

Profile
 
 
Posted: 16 May 2008 12:23 PM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15489
Joined  05-15-2004

You see what’s happening in your post? [ i] is parsed as “start italic”. There is a feature request to turn off pmCode parsing, but for now, your options are, a) use different variable names, or b) surround the whole block with [ code] ... [/ code] tags.

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.

Profile
MSG
 
 
Posted: 16 May 2008 01:28 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  9
Joined  04-01-2008

Alright, thanks everyone.  I understand what the issue is now.

For reference, I also got around the problem but changing

[i]

to

[i+0]

which doesn’t affect the code, but disables the conversion.

Profile
 
 
Posted: 16 May 2008 01:30 PM   [ Ignore ]   [ # 7 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15489
Joined  05-15-2004

Yes, but it might require some explaining, since there is no obvious need from a programmatical point of view… Either way, glad you’re squared away. Closing.

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.

Profile
MSG
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65087 Total Logged-in Users: 39
Total Topics: 82227 Total Anonymous Users: 23
Total Replies: 441925 Total Guests: 224
Total Posts: 524152    
Members ( View Memberlist )