HI WELCOME TO SIRIS
Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

how to redirect one url to another with javascript

Leave a Comment
You can redirect a web page to another page in a number of ways including server-side redirects, HTML meta refresh redirects andJavaScript redirects. In this tutorial, we will demonstrate how you can easily redirect a page using JavaScript by giving examples for various scenarios. Before we move on to our examples though, let’s briefly mention about the importance of using such redirects responsibly and only when you really have to.
Interruptive or unexpected web page redirects are considered to be very annoying from the user perspective, since they negatively affect the overall user experience. For example, if you redirect the users to another website as soon as they land on your site, that will obviously cause frustration for them. Also, if you take the users to an irrelevant page after a timer, after they click on a button or image, or after they complete a certain action on your site, that will most probably result in the user wanting to leave your site at once and never come back.
Another reason you should avoid redirects where possible is that the search engines do not favor websites that use redirects, especially if they are deceptive. Your site may even get removed from their index if you have redirects on your website that try to trick the search engine bots or that result in a bad user experience. Having said that, there are also some cases where a redirect can be quite useful based on the context of your web page or a specific feature of your web application and we will leave the decision whether to use a redirect or not, to you.
Keeping the above points in mind, we can now continue with discussing about how JavaScript redirects work.

JavaScript Redirect Methods

You can redirect a web page via JavaScript using a number of methods. We will quickly list them and conclude with the recommended one.
In JavaScript, window.location or simply location object is used to get information about the location of the current web page (document) and also to modify it. The following is a list of possible ways that can be used as a JavaScript redirect:
Though the above lines of JS code accomplish a similar job in terms of redirection, they have slight differences in their usage. For example, if you use top.location redirect within an iframe, it will force the main window to be redirected. Another point to keep in mind is that location.replace()replaces the current document by moving it from the history, hence making it unavailable via the Back button of the browser.
It is better to know your alternatives but if you want a cross-browser compliant JavaScript redirect script, our recommendation will be to use the following in your projects:
Simply insert your target URL that you want to redirect to in the above code. You can also check this page to read more about how window.location works. Now, let’s continue with our examples.

JavaScript Redirect: Redirect the Page on Page Load

In order to redirect the user to another website immediately after your website is opened, you can use the following code at the top of your page, within the <head> element. Or, if you are using a separate .js file, put the following code into that file and remember to link to it from the head of your page.
Simply replace the example URL with the one you want to redirect to. Note that, with this type of redirection, the visitors will not see your web page at all and be redirected to the target URL instantly.

JavaScript Redirect: Redirect the Page After a Certain Period

In order to redirect the user to another website after a certain time passes, you can use the following code:
The above function will redirect the page 3 seconds after it fully loads. You can change 3000 (3 x 1000 in milliseconds) as you wish.

JavaScript Redirect: Redirect the Page After an Event or User Action

Sometimes, you may want to send the user to another page after a certain event or action takes place, such as a button click, an option selection, a layout change, a form submission, a file upload, an image drag, a countdown timer expiration or things like that. In such cases, you can either use a condition check or assign an event to an element for performing the redirect. You can use the following two examples to give you a basic idea:
The above code will do the redirection if the condition is true.
The above code will do the redirection when the user clicks the #buttonelement.
This is how JavaScript redirect basically works. We hope that the above examples will help you while handling your web page redirects.

For a purely HTML solution, you can use a meta tag in the header to "refresh" the page, specifying a different URL:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com/somepage.html">

write a custom reusable function to populate a dropdownlist

Leave a Comment


Write a custom function in c-sharp. The custom function parameters should be an instance of a dropdownlist, an xml file and a string. 
1. The function should be capabale of populating the passed in dropdownlist.
2. The first item in the dropdownlist should be the passed in string parameter.
3. The data for the dropdownlist comes from the passed in XML file.

The idea is to create a custom function which can be reused through out the project for populating any dropdownlist on any web page. You have 20 minutes to code, test and demonstrate.

The sample code for custom function is shown below. For this example to work drop the XML file in the root folder of the web application.

protected void Page_Load(object sender, EventArgs e)
{
PopulateDropdownlist(DropDownList1, "DropDownListSource.xml", "Select State");
}

public void PopulateDropdownlist(System.Web.UI.WebControls.DropDownList DropDownListObjectToBePopulated,string XMLFilePath, string InitialString)
{
try
{
DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath(XMLFilePath));
if (InitialString != string.Empty)
{
ListItem LI = new ListItem(InitialString, "-1");
DropDownListObjectToBePopulated.Items.Add(LI);
}
foreach (DataRow DR in DS.Tables["State"].Rows)
{
ListItem LI = new ListItem();
LI.Text = DR["StateName"].ToString();
LI.Value = DR["StateCode"].ToString();
DropDownListObjectToBePopulated.Items.Add(LI);
}
}
catch(Exception Ex)
{
}
}

The XML file that has the data for the dropdownlist is as shown below.
<?xml version="1.0" encoding="utf-8" ?>
<StatesList>
<State>
<StateName>Virginia</StateName>
<StateCode>VA</StateCode>
</State>
<State>
<StateName>Iowa</StateName>
<StateCode>IA</StateCode>
</State>
<State>
<StateName>North Carolina</StateName>
<StateCode>NC</StateCode>
</State>
<State>
<StateName>Pennsylvania</StateName>
<StateCode>PA</StateCode>
</State>
<State>
<StateName>Texas</StateName>
<StateCode>TX</StateCode>
</State>
</StatesList>

Explanation of the code:
1.
 PopulateDropdownlist function has 3 parameters. DropDownList to be populated, the path of the XML file which has the data for the dropdownlist and the initial string.

2. Create an instance of DataSet. In our example the instance is DS.
DataSet DS = new DataSet();
3. Read the XML data into the dataset instance using ReadXml() method. Pass the path of the XML file to ReadXml() method. We used Server.MapPath() method to return the physical file path that corresponds to the specified virtual path on the web server.
DS.ReadXml(Server.MapPath(XMLFilePath));
4. We now have the data from the XML file in the dataset as a DataTable.

5. Check if the InitialString is empty. If not empty create a new ListItem object and populate the Text and Value properties. Then add the listitem object to the dropdownlist.
if (InitialString != string.Empty)
{
ListItem LI = new ListItem(InitialString, "-1");
DropDownListObjectToBePopulated.Items.Add(LI);
}

6. Finally loop thru the rows in the DataTable and create an instance of ListItem class. Populate the Text and Value properties to StateName and StateCode respectively. Finally add the ListItem object to the dropdownlist.
foreach (DataRow DR in DS.Tables["State"].Rows)
{
ListItem LI = new ListItem();
LI.Text = DR["StateName"].ToString();
LI.Value = DR["StateCode"].ToString();
DropDownListObjectToBePopulated.Items.Add(LI);
}

7. Drag and drop the dropdownlist on a webform. Call the PopulateDropdownlist() custom function in the Page_Load event handler. When you call the custom function pass the dropdownlist to be populated, XML file path and the initial string.
protected void Page_Load(object sender, EventArgs e)
{
PopulateDropdownlist(DropDownList1, "DropDownListSource.xml", "Select State");
}