Oct 5, 2009
Function to write the cookies in the browser for infinite time in javascript
Reading the cookies from the browser in javascript
Here the function is called on page load
-------------------------------------------------------------------
Sep 25, 2009
Exploring “Location” object in javascript
Javascript location object is the property of the window property.
Properties
- hash - The URL anchor part including the leading hash mark if one exists This is the part of the URL that is used to point to point to a particular part of a page where a named anchor is. The hash is the part containing the # sign that points to the particular page location.
- host - The URL hostname and port. The URL http://spvm:1234/pages/default.aspx has the host value of http://spvm:1234. The colon and port is only included when specified. The URL http://spvm:1234/pages/default.aspx has the host value spvm:1234.
- hostname - The URL hostname section
- href - The entire URL. The following code will load the home default page:
location.href = "http spvm:1234.com/"
The following code will display the URL of the current page:
document.write(location.href)
- pathname - The URL pathname section
- port - The URL port section.
protocol - The URL protocol section including the colon after the protocol name. The values are normally http: or file - search - The URL query string section. This is the section after and including the question mark.
Methods
- reload() - The current window document is reloaded. If a value of true is passed to the reload function, the reload is forced to do an HTTP GET of the document. This is not normally done but is useful when you think the server contents may be different from your cache.
- replace(URL) - Requires a URL as a parameter. It loads the document at the URL on top of the current document. A new entry is not placed in the history object.
Calling javascript from the XSLT on page load.
This is the first article in exploring the power of javascript when called from XSLT.
XSLT is basically used to render the xml in specified format.Here the requirement is to call some javascript function when xslt is rendering the xml.
In this article we are calling javascript on page load .
Scenario : On page load we want to fill some value in text box coming coming from the property defined in the xml .So first we need to access the property value inside javascript and then on page load need to fill the value inside the textbox.
Here is the sample javascript to be used inside the sample.xsl file
<xsl:template name="picture">
<script type="text/javascript">
function myFunction()
{
var url = ']]><xsl:value-of select="substring-before(@PictureUrl, ',')" />';
var txtBox = document.getElementById('textBox');
txtBox.value = url;
}
_spBodyOnLoadFunctionNames.push("myFunction");
]]>
script>
xsl:template>
Keep watching this space for more in javascript and XSLT.If you have done something then please share.
Sep 19, 2009
Hide the template from the site templates ( on create site page) through java script.
Now the problem was the required template was available under the custom template section ,so on page load first template section comes as default selected,so i have to select the appropriate template to check the available templates.
I tried to hide the template through object model ,but through object model I can remove the template permanently from the site,but here i have to hide the template.
So I tried this objective using javascript on the /_layouts/create.aspx page,which is not although not recommended to modify the OOB pages,To insert the javascript in to the page through HTTPmodule without touching the OOB page follow this post Link ,
but again challenge was this "Input Template Picker" control is a user control and on page loads it displays the template in the first tab only,so i checked the page view of the page and it contains only the value in the values in the first tab,
on clicking the next tab,control fires event through javascript and populates the control,so I was not able to capture the event.
Then while surfing I came across one blog which mentions about capturing the event when there is any change in page object,Bingo...now when i selects another tab , I was able to capture the event and write my logic to hide the template.
Here the javascript code contains an "onDocumentChange" DOM function which gets called when something changes on the page,Inside there is another delegate function which contains the logic is to get the collection of all the available option tag and then make the outerHTML value to empty.
Note: Here is another catch,first i tried to make the optionTag[i].Text to empty but what happened in the template selection panel,value disappeared but it still occupies the space and highlights on selection,so I make the outerHTML of the optionTag to empty.
So your job is done!!!
If you have any other method to do the same throug object model or through javascript then do reply
Enjoy!!!
How to get the Query string value through javascript
Requirement is to get the querystring value through javascript.
Here Querystring variable name is “Tag”
Add this function in the javascript block of the page and call according to the requirement.I am calling this function on page load.
Here is code:
-----------------------------
function querySt() {
var url = window.location.search.substring(1);
var qSColl = url.split("&");
for (i=0;i<qSColl.length;i++) {
qsValue = qSColl[i].split("=");
if (qsValue[0] == 'Tag') {
var tabValue = qsValue[1];
}
}
}
---------------------------------
So this way you can access any variable value from the querystring
So your job is done
Enjoy!!!
Limiting length of Multiline textbox(inside webpart) through javascript
So we fix this problem using java script
Add this javascript in the CreateChildControl method.
Here is javascript for this :
----------------------------------------------------------
StringBuilder javascript = new StringBuilder();
javascript.AppendLine("<script type='text/javascript'>");
javascript.AppendLine("function Validation(evt,maxLength)");
javascript.AppendLine("{");
javascript.AppendLine("if(evt.value.length > maxLength)");
javascript.AppendLine("{");
javascript.AppendLine("alert('Maximum 255 characters allowed');");
javascript.AppendLine("evt.value = evt.value.substring(0,maxLength);");
javascript.AppendLine("}");
javascript.AppendLine("}");
javascript.AppendLine("</script>");
if (javascript != null)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "javascriptcode", javascript.ToString());
}
-------------------------------------------------------------
In above code we have written the java script and registered the client script in the page.Now we need to add this javascript event in the multiline textbox
-------------------------------------------------------------txtMultilineTextBox.Attributes.Add("onKeyDown", "Validation(this, 255)");
----------------------------------------------------------------