/    Sign up×
Community /Pin to ProfileBookmark

First post – javascript question

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:-

[CODE]
function highlight(pg,onoff) {//highlight button text
var txt=”test”;
if (onoff) txt=”<font color=&#34;”+document.labelon+”&#34;>”+document.label[pg]+”</font>”;
else txt=”<font color=&#34;”+document.labeloff+”&#34;>”+document.label[pg]+”</font>”;
if (navigator.appName==’Netscape’) with (find(“label”+pg).document) {write(txt); close()}
else find(“label”+pg).innerHTML = txt;
}
[/CODE]

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

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisDec 03.2002 — [i]Originally posted by brutfood [/i]

[B]Question 2: Is it possible to remove buttons and borders, scrollbars etc. from a window after it's been opened?[/B][/QUOTE]


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.
Copy linkTweet thisAlerts:
@jbartoDec 03.2002 — 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. ?
Copy linkTweet thisAlerts:
@AdamGundryDec 03.2002 — 
function highlight(pg,onoff) {//highlight button text

var txt="test";

if (onoff) txt="<font color=&#34;"+document.labelon+"&#34;>"+document.label[pg]+"</font>";

else txt="<font color=&#34;"+document.labeloff+"&#34;>"+document.label[pg]+"</font>";

if (navigator.appName=='Netscape') with (find("label"+pg).document) {write(txt); close()}

else find("label"+pg).innerHTML = txt;

}
[/QUOTE]


In JavaScript, you are allowed to use both single and double quotes (' and ") to denote strings. So you should be able to do this:

[CODE]
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;
}
[/CODE]


I think that's right, but there might be slight errors.

Good luck

Adam
Copy linkTweet thisAlerts:
@Da_WarriahDec 03.2002 — another probably easier way to do it would be this way, escaping the quotes:

[CODE]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;
}[/CODE]


i think either way will work fine, but IMO this way would be easier, take your pick?
Copy linkTweet thisAlerts:
@brutfoodauthorDec 03.2002 — 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.)
Copy linkTweet thisAlerts:
@jbartoDec 04.2002 — [i]Originally posted by brutfood [/i]

[B]Apologies jbarto for my messy code. [/B][/QUOTE]


It's all good! ?
Copy linkTweet thisAlerts:
@AdamGundryDec 04.2002 — [i]Originally posted by brutfood [/i]

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][/QUOTE]


I think you can create a handle to a blank window and use window.document.write to add the HTML.

[CODE]
var SomeHTML = "<b>Some bold text</b>";
var Popup = window.open("");
Popup.document.write(SomeHTML);
[/CODE]


You might need to specify some properties of the popup window. There is help available [URL=http://developer.netscape.com/docs/manuals/communicator/jsref/win1.htm#1152528]here[/URL].

Hope this helps

Adam
Copy linkTweet thisAlerts:
@brutfoodauthorDec 05.2002 — 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)
Copy linkTweet thisAlerts:
@AdamGundryDec 05.2002 — 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 [URL=http://developer.netscape.com/docs/manuals/communicator/jsguide4/sec.htm#1015075]here[/URL].

I'm afraid I can't suggest a workaround, apart from using resizeTo().

Sorry

Adam
×

Success!

Help @brutfood spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...