2

I have a SharePoint site Page in which i need to put an iFrame element to display a web page.To achieve that I wrote some html and javascript within CDATA tag inside the Content section of a ContentEditorWebPart as follows.

<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
<![CDATA[
<iframe width="100%" id="mySitecontainer" src="https://mywebsite/Login.aspx?loguser=" frameborder="0" > </iframe>
<script type="text/javascript">
    var lusr = getLoggedUser(); 
    var requiredUrl = "https://mywebsite/Login.aspx?loguser=" + lusr;
    $('#mySitecontainer').attr('src',requiredUrl);
</script>
]]>
</Content>

Here the getLoggedUser() function is inside an external js file for which I have added a reference earlier.

Now the problem is the above script is not working sometimes, especially whenever I refresh the SharePoint site page in the browser and the src of the above iFrame element is being rendered without any querystring parameter(loguser in this case). Can someone help me where I did wrong here?

6
  • I would run a debug session in you browser - Is your code being hit on refresh or is it skipping it? If you have an error in your getLoggedUser(). My guess here is you are returning a null or undefined reference back for your use and that is killing your JS - so the SRC never gets updated. Run a browser debug session (F12) and set some breakpoints on the above to see what is happening and let us know what is being returned. Commented Jun 29, 2014 at 14:14
  • Hey @JesusShelby Of course i did the browser test with developer Tools(F12) and realized that the above script is not running somehow in the Page Refresh(F5). But when i re-enter the url of my page in the adress bar(this not page refresh) i.e if i made a new request to the same site its working then.I am unable to find out wats going on. Commented Jun 29, 2014 at 14:47
  • right - but I'm not still not clear. Is it not executing at all (none of the script block) OR is it executing and failing? Commented Jun 29, 2014 at 15:59
  • @Jesus Shelby it is not executing at all. Commented Jun 29, 2014 at 16:06
  • Hey @JesusShelby isn't there any other to put iframe without using CDATA i mean can i put html and script for iframe directly without using CDATA tag ? Commented Jun 29, 2014 at 16:08

3 Answers 3

2

SharePoint may re-write your code in the content editor webpart. I suggest you take one of two approaches:

In 2010/2013 you can create a txt file and store in a library. I suggest you place your code in that file and use the Content Link property of the Content Editor Web Part. This will avoid SharePoint overwriting your code or doing something funky behind the scenes as it parses the CEWP. This also makes it easier to test and debug from the browser as you don't have to edit the page to see to your changes.

In 2013 - you also have the Script Web Part. You can put your HTML in the content editor web part and then your javascript in the script web part. you still need to add in the <script></script> tags when using this web part.

I've found using the content editor web part directly with embedded code can be tricky. In some cases where your JavaScript changes the HTML, that can actually get saved back to the content editor web part. I've seen this for instance when embedding a twitter feed, where the DIV placeholders are replaced with actual layout code that is injected via JavaScript.

1
  • Hi @Jesus Shelby thanks for your answer. As i am oof now i can't try it now. But i'll give it a try and get back to you if it works fine. Commented Jun 29, 2014 at 16:37
2

May your problem is caused by MDS (minimal download strategy). Since your code is on the page but is not interpreted again, you have to attach it to the page lifecycle.

For example with JQuery:

$(function () {
      ExecuteOrDelayUntilScriptLoaded(function () {
          if (typeof asyncDeltaManager != "undefined")
            asyncDeltaManager.add_endRequest(myFunction);
          else myFunction();
      }, "start.js");

  });

function myFunction() {
  //your code
}

Here's some reference for this.

Or without it:

function $_global_customjs(){ 
    _spBodyOnLoadFunctionNames.push('DoSomething'); 
} 
    var DoSomething = function () 
    { 
        // --- Your custom JS here 
    } 
$_global_customjs();

Here's some reference for this.

5
  • hey @Max thanks for the useful answer. Here i am perfectly able to change the src attribute of iFrame after i made some changes, even at so many refreshes. Commented Jul 6, 2014 at 9:33
  • But the problem is sometimes the ASP.NET pages are being displayed by a different query string parameters being append to their page urls. Commented Jul 6, 2014 at 9:34
  • I came to know that these wrong values are nothing but the returned values of a user who accessed the SharePoint site page simultaneously.Even i though the src attribute of iFrame is pointing correct url with exact query string parameters(i am sure about this because i looked into it by Developer tools and its correct). So i thought either the iFrame is being cached or in between the gap of javascript function execution to form src of iframe and to load the content with pointing to the formed url somehow the content page is being loaded with wrong url which doesn't make any sense. Commented Jul 6, 2014 at 9:34
  • So my iFrame is having correct src attribute with a correct Query string parameter but the page loaded by iFrame is having some other user's query string parameter.Can you help me with this ? Commented Jul 6, 2014 at 9:38
  • If your IFrame is having the correct src and querystring, then the page it displays should get the correct querystring too. If thats not the case, there may is another problem.
    – Mx.
    Commented Jul 6, 2014 at 11:57
0

The Content Editor Web Part is intended for adding HTML content to a Web Part Page, it is not designed to link to a Web site. If you need to link to a Web site, consider using the Page Viewer Web Part. -Microsoft- http://office.microsoft.com/en-us/windows-sharepoint-services-help/about-the-content-editor-web-part-HA001160897.aspx

2
  • Milind, please don't post duplicate answers. Thanks.
    – Benny Skogberg
    Commented Jun 29, 2014 at 9:34
  • @Milind Thanks for the immediate reply. But the Content Editor Web Part is working fine for me. I am getting problem only for the QueryString parameter.Can you suggest anything related this Content Editor Web Part only as i don't have enough time to redesign by replacing CEWP with Page Viewer Web Part ? Commented Jun 29, 2014 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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