Javascript in C#.Net web application

Discussion in 'Software' started by MBlock, Aug 17, 2007.

  1. MBlock

    MBlock Private E-2

    Hey everyone,

    I have what I imagine is a pretty basic question but I am just getting into javascript. I want to create a new line break in a javascript message for my web app I am working on. Right now the dialog box pop up is too long and I want to just have two lines of text instead of all on one. The code I have is:

    string sJavaScript ="";
    sJavaScript = "<script language='javascript'>";
    sJavaScript += "alert('Line1 '\n' Line2');";
    sJavaScript += "</scr" + "ipt>";
    Response.Write(sJavaScript);

    When I run that IE gives me a javascript error saying:

    Line: 2
    Char: 1
    Error: Expected ')'
    Code: 0

    And no pop-up is displayed. if I remove the '\n' part than it displays just fine... but all on one line. if I remove the two apostrophe's and just have the \n in between the lines than I get an error that says "Unterminated string constant" and Line: 1 and Char: 78


    Please ANY insight or advice as soon as possible would be greatly appreciated! TIA!
     
  2. MBlock

    MBlock Private E-2

    Problem solved. Just in case anyone else ever comes across this problem. The solution is as follows:

    Since I was using Visual Studios and adding all the J Script to a string variable THEN passing the string into it's runtime what I had written was " Line 1 \n Line 2" this was merely creating a new line in the runtime and not being processed as a new line in the alert box so the escape sequence of \n was not actually getting written into the runtime. So I simply needed to dereference the '\n' operation by using the following sintax

    alert('Line 1 \\n Line 2')

    That way the \n was getting carried through the string creation and being passed into the runtime.
     
  3. Kodo

    Kodo SNATCHSQUATCH

    string sJavaScript ="";
    sJavaScript = @"alert('Line1 \n\r Line2');";
    ClientScript.RegisterScriptBlock(this.GetType(),"__test",sJavaScript,true);
     
    Last edited: Aug 25, 2007

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds