Insert JavaScript into a Content Editor Web Part (CEWP)

Posted: 08/31/2010 in Customization

In the 2007 version of SharePoint, we had the Source Editor included in the Content Editor Web Part (CEWP) as our way of inputting JavaScript directly onto a page. The process on how to do this has changed a little bit in the new 2010 version. Follow below on how to successfully perform the same task.  Read more…

Advertisement
Comments
  1. Ashok says:

    Hi,

    As you said,I tried to hide context menu for a list in sharepoint 2010, but it’s not working.

    Java script is:

    function CAMOpt(p,wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc)
    {
    var mo=CMOpt(wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc);
    if(!mo)return null;
    if(wzText != “View Item”)
    if(wzText != “Edit Item”)
    if(wzText != “Compliance Details”)

    if(wzText != “Manage Permissions”)
    if(wzText != “Delete Item”)
    AChld(p,mo);
    return mo;
    }

  2. Anyone know how to modify this idea “Collapse text in list view” (http://www.endusersharepoint.com/2009/02/20/jquery-for-everyone-collapse-text-in-list-view/)to work in SP2010?

  3. Anneela says:

    Hi, the solution above works great. However it does not work at all for the javascript that I am using that rotates images. Do I need to fix the code or can you assist?

    <!–
    function adArray() {
    for (i=0; i*2<adArray.arguments.length; i++) {
    this[i] = new Object();
    this[i].src = adArray.arguments[i*2];
    this[i].href = adArray.arguments[i*2+1];
    }
    this.length = i;
    }
    function getAdNum() {
    dat = new Date();
    dat = (dat.getTime()+"").charAt(8);
    if (dat.length == 1)
    ad_num = dat%ads.length;
    else
    ad_num = 0;
    return ad_num;
    }
    var ads = new adArray(
    "/SiteAssets/welcome_img01.png","http://pos-shp-11&quot;,
    "/SiteAssets/welcome_img02.png","http://pos-shp-11&quot;,
    "/SiteAssets/welcome_img03.png","http://pos-shp-11&quot ;) ;
    var ad_num = getAdNum();
    document.write('‘);
    link_num = document.links.length-1;
    function rotateSponsor() {
    if (document.images) {
    ad_num = (ad_num+1)%ads.length;
    document.js_ad.src = ads[ad_num].src;
    document.links[link_num].href = ads[ad_num].href;
    setTimeout(“rotateSponsor()”,4000);
    }
    }
    setTimeout(“rotateSponsor()”,4000);
    // –>

    • sptwentyten says:

      I am by NO means a developer. :) However, perhaps someone will see this and provide assistance. Also, there is a Slideshow Web Part OOTB in 2010 now…have you tried just using that?

  4. jamiep77 says:

    I am trying to use a twitter widget and have followed the instructions above, pasting the following Javascript into a text file and then calling it from the Content Editor web part:

    new TWTR.Widget({
    version: 2,
    type: 'profile',
    rpp: 4,
    interval: 6000,
    width: 250,
    height: 300,
    theme: {
    shell: {
    background: '#6795db',
    color: '#ffffff'
    },
    tweets: {
    background: '#d9e5f7',
    color: '#000000',
    links: '#0982d9'
    }
    },
    features: {
    scrollbar: true,
    loop: false,
    live: true,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
    }
    }).render().setUser('jamiep77').start();


    When I do this, the widget shell appears, but no dynamic content will work. Am I doing something wrong?

  5. Richard says:

    I have a question, I am lookin for any tutorial how to use javascripts in CEWP directly. For example I have table with buttons. Every button have to call different function after click on it. So onclick will show up some div, or hide. Javascript source is loaded on master page. So It should be workin. I just need to add that function for my buttons. Pciture, or div or any aprt I want. Can anybody help me how to make it? If I try to add function directly in “Edit HTML source” it doesn’t work. So my div looks like ” BOOK and I need to be like this BOOK PLEASE HELP

  6. Bil Simser says:

    Just a reminder which may fix some of the peoples problems here. When you use a content link it’s effectively bringing in the file contents and dropping them on the page. So if you’re linking to javascript you have to include the [script] tags surrounding your code.

  7. S D says:

    How do you get it to keep from putting the tags around your code? It’s messing up my page :(

  8. Thank you for posting this. It helped removing the bug “The HTML has been modified” meaning all the javascript had been removed.

    I had another problem using the SharePoint client API that appeared on some websites. Some times the SP. ClientContext was null.
    In order to fixed this error I used this code.

    var clientContext;

    ExecuteOrDelayUntilScriptLoaded(initialize,”SP.js”);

    function initialize() {

    this.clientContext = SP.ClientContext.get_current();
    }

  9. ds says:

    It worked for me, thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s