views:

49

answers:

5

I want pass a parameter of javascript functiona which is a string. This javascript function is a hintbox on mousehover..

and the string i am using is like this:

Hemmed Finish: Every side/edge (1/2" to 2") of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.

Stitched Finish: Every side/edge (1" to 2") of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner.

Now in the hintbox on mousehover the above given text has to be display as it is displayed above along with the paragraph break.. But when i pass the above as parameter in that function along with appending some backslashes to recognise some punctuation, iots till gives me javascript error of unterminated string...

I am doing this:

onMouseover="showhint('Hemmed Finish\: Every side/edge \(1/2\'\' to 2\'\'\) of the banner are folded and glued \(special vinyl solution\) or heat pressed. This is the most common and best finish option.Stitched Finish\: Every side/edge \(1\'\' to 2\'\'\) of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner', this, event, '250px')"

pls could u help me in rectifying the issue...

+1  A: 

Check this :

onMouseover="showhint('Hemmed Finish: Every side/edge (1/2\'\' to 2\'\') of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.Stitched Finish: Every side/edge (1\'\' to 2\'\') of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner', this, event, '250px')"

you do not need to escape : brackets like (

<script type="text/javascript">
<!--//
function alert2()
{
alert ("Don't forget the curly "+
"brackets - \nThey are essential!");
}
//-->
</script>
Pranay Rana
Pranay I need the tooltip to display the string in 2 different paragraphs i have given in question..
OM The Eternity
use \n than check updated ans
Pranay Rana
thanks pranay for directing in right way
OM The Eternity
A: 
dejanb
The `"` terminates the **attribute** value and `\` is not an escape character in HTML.
David Dorward
+1  A: 

I see two potential problems here.

  1. JavaScript, like many other programming languages, uses the " character to mark the beginning and the end of a string. Your hintbox text contains a ", so when JavaScript encounters it, it thinks that your string has ended. To fix the problem, you should refer to each " in your string as \". This tells JavaScript that you don't intend to end the string, but rather you plan to put a " inside your string.

  2. Similarly, JavaScript does not allow you to place a "paragraph break" directly inside a string. Instead, you must type \n each time you want a carriage return inside your string.

So, here is how you should define your hintbox string:

hintbox = "Hemmed Finish: Every side/edge (1/2\" to 2\") of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.\n\nStitched Finish: Every side/edge (1\" to 2\") of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner.";

This is typically called "escaping strings", and a quick Google should give you much more information on the toppic.

Happy programming!

Mike
Thanks Mike....
OM The Eternity
+3  A: 

Ok, I have the solution:

<element onMouseover="showhint('&lt;p&gt;Hemmed Finish: Every side/edge (1/2&quot; to 2&quot;) of the banner is folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.&lt;/p&gt;&lt;p&gt;Stitched Finish: Every side/edge (1&quot; to 2&quot;) of the banner is folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner&lt;/p&gt;', this, event, '250px')" />

You have to escape the quotes XML-style, using &quot;.

See a working example here.

Eric
HTML style. It just happens to be the same as XML style.
David Dorward
I thought the relationship was the other way around.
Eric
I checked ur code and its not giving me two different paragraphs as i am getting using a single <p> as i did
OM The Eternity
first of all look out the question properly I was just asking for the solution to display the paragraph change, not the other hocuspocus....
OM The Eternity
Didn't the one at the link work?
Eric
no bud it didnt work as i need it
OM The Eternity
@eric now pls my updated answer... i know using tags is still unvalid as u r saying but this time i have closed the tag as per requirement of validation and removed the unnecessary backslashes.
OM The Eternity
How did you need it to work? All you need to do is substitute `document.write` for your function
Eric
@Eric — No, SGML came first, HTML 3/4 are applications of SGML and defined `quot` in their DTDs, XML came after HTML 4 and made `quot` a built in.
David Dorward
Oh, OK. Thanks for clearing that up.
Eric
+1  A: 

Whilst the example works for me, it's pretty ugly. Avoid inline event handlers and the crazy escaping problems that come with them. Put the data elsewhere, such as in a JS variable or an attribute value where appropriate. For the case of a tooltip, the title attribute is best:

<span title="Hemmed Finish: Every side/edge (½″ to 2″) of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.&#10;&#10;Stitched Finish: Every side/edge (1″ to 2″) of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner.">
    finish options...
</span>

Whilst that works as a tooltip in itself, you can use progressive enhancement to replace that native-HTML tooltip with a scripted one if you want eg. the more flexible styling that comes with it:

var spans= document.getElementsByTagName('span');
for (var i= spans.length; i-->0;)
    if (spans[i].title)
        replaceTitle(spans[i]);

function replaceTitle(element) {
    var title= element.title;
    element.title= '';
    element.onmouseover= function(event) {
        showhint(title, this, event||window.event, '250px');
    };
    element.onmouseout= function(event) {
        hidehint(...);
    };
}
bobince