Execute Javascript in Sharepoint Webpart
Locked
-
Monday, June 09, 2008 2:07 PM
I am trying to run javascript from a .js file in my webpart. The webpart shows and looks great, but none of the javascript works. The javascript works perfect in a normal html page. I am using writer.write to write the html to the page (see code below). The .css files and images work and show properly. The path for the .css, .js and images are correct.
I have not been able to find any answers to this problem, any help would be greatly apprecicated.
protected
override void Render(System.Web.UI.HtmlTextWriter writer){
string product_id = Page.Request.QueryString["ProductID"];writer.Write(
"<html xmlns='http://www.w3.org/1999/xhtml'><head>" + "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />" + "<title>Title</title>" + "<link href='/assets/css/product_detail.css' rel='stylesheet' type='text/css' />" + "<link href='/assets/css/tier3.css' rel='stylesheet' type='text/css' />" + "<link href='/assets/css/menu.css' rel='stylesheet' type='text/css' media='screen' />" + "<script type='text/javascript' src='/assets/scripts/p7tpscripts.js'></script>" + "<script type='text/javascript' src='/assets/scripts/jquery-1.2.3.min.js'></script>" + "<script type='text/javascript' src='/assets/scripts/hoverIntent.js'></script>" + "<script type='text/javascript' src='/assets/scripts/menu.js'></script>" + "</head>" + "<body>" + "<div id='content'><div id='main'><div id='tabs'><div id='p7TP1' class='p7TPpanel'><div class='p7TPwrapper'><div class='p7TP_tabs'>" + "<div id='p7tpb1_1' class='down'><a class='down' href='javascript:;'>Description</a></div>" + "<div id='p7tpb1_2'><a href='javascript:;'>Specifications</a></div>" + "<div id='p7tpb1_3'><a href='javascript:;'>Accessories</a></div>" + "<div id='p7tpb1_4'><a href='javascript:;'>Documentation</a></div>" + "<div id='p7tpb1_5'><a href='javascript:;'>Colors</a></div>" + "<div id='p7tpb1_6'><a href='javascript:;'>Images</a></div>" + "<div id='p7tpb1_7'><a href='javascript:;'>How to Buy</a></div>" + "<br class='p7TPclear' />" +
All Replies
-
Monday, June 09, 2008 2:38 PM
You need to understand the concept of WebPart. What you are trying to do is emit HTML of whole of the page using javascript.
Do not get confuse WebPart class with the same as Page Class. WebPart is hosted inside a web part page which has its own HTML emitted.
To inject javascript inside webpart, use standard .NET 2.0 methods RegisterClientScriptBlock.
Also see:
http://msdn.microsoft.com/en-us/library/ms948916.aspx
--
Madhur -
Wednesday, May 06, 2009 9:18 PMMadhur,
Using RegisterClientScriptBlock will inject the javascript somehwere on the page and not necessarily inside the web part. Is there any way to inject the javascript directly inside the web part?
Thanks, -
Monday, February 07, 2011 5:17 PM
Try the following. I have used this successfully.
protected override void Render(HtmlTextWriter writer)
{
string script;script = @"<script language=javascript>
//your javascript here
</script>";writer.Write(script);
base.Render(writer);
}

