Click to See Complete Forum and Search --> : First post - javascript question
brutfood
12-03-2002, 02:24 AM
Hi everyone, nice forum.
I'll launch straight into my questions. I've written some Javascript to do a text rollover. But the quotes in the HTML string are causing problems. This is what I have:-
function highlight(pg,onoff) {//highlight button text
var txt="test";
if (onoff) txt="<font color=""+document.labelon+"">"+document.label[pg]+"</font>";
else txt="<font color=""+document.labeloff+"">"+document.label[pg]+"</font>";
if (navigator.appName=='Netscape') with (find("label"+pg).document) {write(txt); close()}
else find("label"+pg).innerHTML = txt;
}
Labelon and labeloff contain strings of the form "#FF0000" (red). The label array contains text.
How do I get round my quote problem?
Question 2: Is it possible to remove buttons and borders, scrollbars etc. from a window after it's been opened?
Thanks
gil davis
12-03-2002, 05:41 AM
Originally posted by brutfood
Question 2: Is it possible to remove buttons and borders, scrollbars etc. from a window after it's been opened?
In IE, there is no way to access those parameters for an existing window. They only work when using window.open(). In fact, you cannot even tell if the window you are in has any of the features turned on.
Netscape 4.x allows some control over them. At least you can see if they are on.
Since the window object is not covered by W3C specifications, all browsers (and even different platforms) have different features and support.
jbarto
12-03-2002, 07:28 AM
I can't really tell from your code what the problem is. If you could clean it up just a little bit, we might be able to see what the problem is. :D
AdamGundry
12-03-2002, 01:35 PM
function highlight(pg,onoff) {//highlight button text
var txt="test";
if (onoff) txt="<font color=""+document.labelon+"">"+document.label[pg]+"</font>";
else txt="<font color=""+document.labeloff+"">"+document.label[pg]+"</font>";
if (navigator.appName=='Netscape') with (find("label"+pg).document) {write(txt); close()}
else find("label"+pg).innerHTML = txt;
}
In JavaScript, you are allowed to use both single and double quotes (' and ") to denote strings. So you should be able to do this:
function highlight(pg,onoff) {//highlight button text
var txt="test";
if (onoff) txt='<font color="'+document.labelon+'";>'+document.label[pg]+'</font>';
else txt='<font color="'+document.labeloff+'";>'+document.label[pg]+'</font>';
if (navigator.appName=='Netscape') with (find("label"+pg).document) {write(txt); close()}
else find("label"+pg).innerHTML = txt;
}
I think that's right, but there might be slight errors.
Good luck
Adam
Da Warriah
12-03-2002, 02:02 PM
another probably easier way to do it would be this way, escaping the quotes:
function highlight(pg,onoff) {//highlight button text
var txt="test";
if (onoff) txt="<font color=\""+document.labelon+"\">"+document.label[pg]+"</font>";
else txt="<font color=\""+document.labeloff+"\">"+document.label[pg]+"</font>";
if (navigator.appName=='Netscape') with (find("label"+pg).document) {write(txt); close()}
else find("label"+pg).innerHTML = txt;
}
i think either way will work fine, but IMO this way would be easier, take your pick:D
brutfood
12-03-2002, 05:18 PM
Thanks for the feedback - and escaping the quotes looks neat. Actually, what I've just viewed on this thread isn't what I wrote. I actually represented the quotes by amplersand hash 34. Also, last night I suspected that it wasn't the quotes causing the problem, but the hash in the colour string. HTML seems to understand color="hex" without the hash, so I just took it out.
Apologies jbarto for my messy code.
Another question: Is it possible to pass a string from javascript, containing HTML, to a new window that you open, so that the HTML is displayed? (The alternative would be to pass it back to some server-side code and generate a file.)
jbarto
12-04-2002, 06:29 AM
Originally posted by brutfood
Apologies jbarto for my messy code.
It's all good! :D
AdamGundry
12-04-2002, 11:12 AM
Originally posted by brutfood
Another question: Is it possible to pass a string from javascript, containing HTML, to a new window that you open, so that the HTML is displayed?[/B]
I think you can create a handle to a blank window and use window.document.write to add the HTML.
var SomeHTML = "<b>Some bold text</b>";
var Popup = window.open("");
Popup.document.write(SomeHTML);
You might need to specify some properties of the popup window. There is help available here (http://developer.netscape.com/docs/manuals/communicator/jsref/win1.htm#1152528).
Hope this helps
Adam
brutfood
12-04-2002, 07:37 PM
Thanks Adam,
I always have problems with specifying the width and height of a new window I open in javascript
window.open(myurl,myname,'width=800,height=600');
It never works, although any other parameters I use do work. I always have to do the window.resizeTo(800,600) after the open to get this working. Any suggestions why? (IE5.1 MAC)
AdamGundry
12-05-2002, 03:07 PM
It is not normally possible to open a new window larger than the user's screen (which could be 640 x 480) due to security restrictions on JavaScript. There are various other restrictions, such as not creating windows smaller than 100px square.
In order to avoid this restriction, you must be using a signed script. There is infromation on signed scripts here (http://developer.netscape.com/docs/manuals/communicator/jsguide4/sec.htm#1015075).
I'm afraid I can't suggest a workaround, apart from using resizeTo().
Sorry
Adam