Showing posts with label javaScript. Show all posts
Showing posts with label javaScript. Show all posts

Tuesday, May 24, 2011

Redirects handled by Google Analytics

Google Analytics won't track a visit to a page unless that page runs the analytics tracking code. Although, there are at least 2 ways to track the redirects:
-Tracking a redirect using 301 redirect.
-Tracking a redirect using a JavaScript redirect.

In both instances, make sure the directory actually exists as a file (/it/growthmodel.aspx). I always use the JavaScript redirect so, let's talk about it:
Let say, you have a pdf file (growthmodel.pdf), you want to post it online and also want to count the number of visitors and their behaviours. For this, create a page growthmodel.aspx under the /it directory and redirect this page to pdf file using javascript redirect. Put the below code at growthmodel.aspx page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"
>

var _gaq = _gaq
[];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ?
'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

</head>
<body onLoad="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label'])">
<script type="text/javascript"
>

function redirect()
{
window.location =
"/growthmodel.pdf"
}

var temp = setInterval("redirect()", 1000);

</script>
</body>
</html>

setInterval=> 1000 so that tracking doesn't get missed.
'UA-XXXXXX-X' => please replace xxxx with your actual analytics account number.

Happy Programming!!

Thursday, May 19, 2011

Startup JavaScript Code from Content Page to Master Page

Let say, you have Master and Content pages and you want to add some JavaScript code from your Content pages that would run when the page is loaded. Content pages do not have the HTML elements like body tag to add your script on its onload event.

For this, you can use RegisterStartupScript method. Add the following code to your Content Page code behind Page_Load Event:

protected void Page_Load(object sender, EventArgs e)
{
Type type = GetType();
const string scriptName = "alertPopup";
if (!ClientScript.IsStartupScriptRegistered(type,scriptName))
{
ClientScript.RegisterStartupScript(type, scriptName, "alert('Hello World!')", true);
}

}
Happy Programming!!

Sunday, October 25, 2009

Clear default text onClick - restore if nothing entered

Let's write a JavaScript function which will clear the "default" text in a field when the user clicks into it and the second function will replace the default text in the field if the field was left blank.

Put this inside the <head> </head> tag:

<script type="text/javascript">
function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
</script>

Then add the following onclick, onblur events to your field:

<asp:TextBox ID="rajTextBox" runat="server" Text="Rajendra Sedhain"
Width="369px" Height="18px" onclick="clickclear(this, 'Rajendra Sedhain')" onblur="clickrecall(this,'Rajendra Sedhain')"></asp:TextBox>

Happy Programming !!!

Monday, October 5, 2009

Disabling an ASP.Net Validator through Javascript

Let say, I have two dropdown lists, both have required field validation controls and use Javascript to hide the second dropdown when choose the specific items from the first dropdown list. It works fine but the problem is when choose the specific items from the first dropdown it hides the second one but it still displays the required field validator.

So, the solution:

When we hide, we can disable the validator at the same time using ValidatorEnable().

function validate()
{

var myValidator = document.getElementById('myValidatorClientID');
ValidatorEnable(myValidator, false);

}

To make enable replace that 'false' by 'true'.

Happy Programming !!!

Friday, September 25, 2009

Change Hidden Field Value

Let say, you have a Textbox with some value. When you choose some items from a dropdown list, this textbox will be hidden but the value will be there. When you submit the form, this textbox's value will be submitted.

To avoid this, we can use javascript so that when the textbox hidden, we can make it empty or assign something specific value whatever you want.




function assignSomeValue()
{
// to hide the textbox
document.getElementById('<%= rajTextBox.ClientID%>').style.visibility = "hidden";
// to assign somevalue after hidden
documnet.getElementById('<%= rajTextBox.ClientID%>').value="assign something";


}





Happy Programming !!!

Wednesday, April 15, 2009

Disable Browser Back Button

If you want to disable going back in the previous page, I am going to show you very easy method to do this:

Suppose you are in Page 1 and go to page 2. If you want to go back to page 1 , you are able to go. But now, if you want to disable that going back features, add the following script in the body tag of the page 1:

body onload=”if(history.length>0)history.go(+1);”

This works only for IE and not for Mozilla Firefox. For Mozilla do this:

add this script within head:

function noBack()
{
window.history.forward()
}
noBack();
window.onload=noBack;
window.onpageshow=function(evt)
{
if(evt.persisted)noBack()
}
window.onunload=function()
{
void(0)
}

It works not only with Firefox but also with IE, Safari, Opera.

Sunday, March 15, 2009

Close the popup image when click on it

To close the popped up image when click on , do the following steps:

Create ShowImage.aspx page and add a image control with onclick property like:

asp:Image ID="Image1" runat="server" onclick = "window.open('','_self');window.close();"

And in the page load of ShowImage.aspx do this way:

if (Request.QueryString["image"] != null)
{
Image1.ImageUrl = Request.QueryString["image"];
}

Now, on the parent page write this script:

var Popup;
function popUp(url)
{
Popup = window.open(url, "Popup", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=100,height=100,left = 490,top = 262');
Popup.focus();
}
function ChangeImg(obj)
{
//Show in popup
var radio = obj.getElementsByTagName("input");
for (var i=0;i less than radio.length;i++)
{
if (radio[i].checked)
{
popUp("ShowImage.aspx?image=images/" + radio[i].value + ".jpg");
}
}
}

Note: Use the general sign for 'Less than' in the for loop. I am unable to publish here because it acts like HTML tags and won't let me go.

Wednesday, February 25, 2009

Images changing when clicking the radiobutton on the radiobutton list

If you have a ASP.NET C# form and use a radiobutton list. Suppose there are 4 radiobutton on the list.Default, no images loaded and when click the first radiobutton in the list, it displays a "1.jpg" and when click second radiobutton changes the image and so on...

For this, use the onclick function of the radiobuttonlist.

onclick = "ChangeImg(this);

and try this javascripts:


var Popup;
function popUp(url)
{

Popup = window.open(url, "Popup", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=100,height=100,left = 490,top = 262');
Popup.focus();
}

function ChangeImg(obj)
{
//Show in popupvar radio = obj.getElementsByTagName("input");


for (var i=0;i less than radio.length;i++)
{

if (radio[i].checked)
{
popUp("images/" + radio[i].value + ".jpg");break;
}
}
}

Sunday, February 1, 2009

Auto close Popped up images after certain time

If you want to close your pop up image after 10 sec. just follow me:

Add a javascript function to the popup page
function Close()
{
window.close();
}
And to the body tag onload event add this

onload = "setTimeout('Close();', 10000)"

it will close the popup after 10 secs.