Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

My SharePoint solution has a userControl that Register a js block

string getJS(string title, string htmlContent)
 {
     return (string.Format(@"
          ExecuteOrDelayUntilScriptLoaded(function () {{
                      var options = SP.UI.$create_DialogOptions();
                      options.title = '{0}';
                      options.html = '{1}';
                      options.autoSize = true;
                      options.showClose=true;
                      options.allowMaximize=false;
                      SP.UI.ModalDialog.showModalDialog(options);
            }}, 'sp.js');"
          , title, "<div>Blah blah</div>"));
  }

I have registered the sp.ui.dialog.js to make sure that this function is available

<Sharepoint:ScriptLink ID="slSAAuto" Name="sp.ui.dialog.js" LoadAfterUI="true" Localizable="false" runat="server"></Sharepoint:ScriptLink>

I register my JS block at run time

ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "popupOnPageLoad", getJS(title,htmlContent), true);

after page loads, i got a JS error

Uncaught TypeError: Object <div>Blah blah</div> has no method 'getElementsByTagName'

Where did i go wrong?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Just for starters, I think you have called the ExecuteOrDelayUntilScriptLoaded function in a wrong way.(You have given an extra set of curly braces for the function inside. Try this code:

string getJS(string title, string htmlContent)
 {
     return (string.Format(@"
          ExecuteOrDelayUntilScriptLoaded(function () {
                      var options = SP.UI.$create_DialogOptions();
                      options.title = '{0}';
                      options.html = '{1}';
                      options.autoSize = true;
                      options.showClose=true;
                      options.allowMaximize=false;
                      SP.UI.ModalDialog.showModalDialog(options);
            }, 'sp.js');"
          , title, "<div>Blah blah</div>"));
  }

UPDATE:

Also, options.html needs a valid DOM element. You are passing a string containing <div>Blah Blah</div> right now.

It should be done like this:

var divElem = document.createElement('div');
divElem.innerHTML = 'Blah Blah';

//Rest of your Code.

options.html = divElem;
share|improve this answer
    
the extra braces are for escaping.. inside string.Format you need to escape the { and } else you'll get input is not in a correct format exception –  Rami.Shareef Apr 2 '12 at 6:03
    
but you have already given a @ at the start of the string. Do you still need to escape the { and }? –  Vardhaman Deshpande Apr 2 '12 at 6:05
    
Check out the edit to my answer. –  Vardhaman Deshpande Apr 2 '12 at 6:13
    
Yes you still need to escape { and }, and about you suggestion .. f*** yeah it works :) the DOM element solve the issue, but im wondering what the difference between the div DOM and the <div>...</div>? Thanks –  Rami.Shareef Apr 2 '12 at 6:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.