Question

changing the value of src in script tag based on drop down select event

Asked by: shwaqar82

Hi

How can i change the value of 'src' on drop down select event using javascript...
the src tag is here :
<script type="text/javascript" src="/shared/data/top3/Toronto.js"</script>

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-06-26 at 07:02:20ID22658315
Tags

value

Topics

Programming for ASP.NET

,

JavaScript

Participating Experts
4
Points
500
Comments
16

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. javascript src= in Netscape
    I am trying to reference a *.js file in my HTML pages, so I only have to write the javascript once. I have it working fine in IE 4/5, but Netscape Communicator 4.5 only seems to work if I have the JavaScript in the HTML page. Shouldn't I be able to use the same tag <scri...
  2. simple question about src in JS tag?
    What does it mean (refer to) when there is a source tag in the javascript tag? Example: <SCRIPT LANGUAGE="JavaScript" SRC="../../scc-js/newsroll2.js">
  3. using javascript src tag in servlets for Apache
    i have been trying to call javascript using javascript src tag (<javascript src="script.js">) in my servlet which generates the html page(by using printwriter.println(""); statement) but the generated page fails to call the source. is it that the g...
  4. Change src of a Javascript in Netscape
    I have a script that I want to change the location of to populate arrays with different values. For example, in a select progression of country to city, I don't want have people have to download all of the cities throughout the world (e.g. if I just put al of the arrays in t...
  5. Javascript img tag send src onclick
    I want to gather the src information from a img tag and assign it to a variable and send it to a function. <a href="javascript:void(0)" onclick="alert(document.image[this].src)"><img src="thisimage.jpg"></a> and have the value...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: jasonsbytesPosted on 2007-06-26 at 07:05:29ID: 19363754

I don't know that you can change the src of a script tag using javascript...

 

by: jasonsbytesPosted on 2007-06-26 at 07:07:13ID: 19363772

I think your solution will be to include multiple script tags with all of the required js files included, then you can use whichever one you need.

 

by: ShimonYKPosted on 2007-06-26 at 07:34:04ID: 19364037

If the script runs in response to a user action, for example clicking a button or link, you could write the script to the page as an asp:literal control (http://www.w3schools.com/aspnet/control_literal.asp).  Then you can set the text of the script programatically at run time.

<asp:Literal ID="Literal1" runat="server" Text="<script type='text/javascript' src='/shared/data/top3/Toronto.js'></script>" />


if the script needs to run on page load, you will need to use ClientScript.RegisterStartupScript method to write your script (http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript(VS.80).aspx)

 

by: ThogekPosted on 2007-06-26 at 09:27:10ID: 19365390

Technically, you can reset the src property of a script element, although I'm not so sure that it always works (meaning that it actually triggers the load of the new src URL).  E.g., you can give your script element an ID, like:

    <script id="myScript" type="text/javascript" src="/shared/data/top3/Toronto.js"</script>

and then access it and change its src property just like you would for an image.

    var scriptElem = document.getElementById("myScript");
    scriptElem.src = 'someOther.js';

However, again, while you can do this, when I've tinkered with it, it hasn't always resulted in the new JS actually loading and being usable by the page.

Another option might be to place a block element, like a div, whose innards you can rewrite, like:

    <div id="scriptPanel"></div>

and

    var divElem = document.getElementById('scriptPanel');
    divElem.innerHTML = '<script type="text/javascript" src="someOther.js"></script>';

I don't think I've actually tested that one much in terms of being able to reference functions defined in the someOther.js, but I have seen document.write statements in a JS file loaded this way work just fine (in fact a lot of Web advertising services deliver their ads in a manner similar to this).

Hope some of that helps...

 

by: shwaqar82Posted on 2007-06-26 at 11:43:25ID: 19366617

Error: 'scriptElem' is null or not an object
thats waht it gives me a messagewhen i implement ur first option...the 2nd option is not working at all..

Thanks for your help but ...can you help me fix this problem.
Best Regards
Shaukat Waqar

 

by: ThogekPosted on 2007-06-26 at 21:08:43ID: 19369646

Kinda hard to fix a problem I can't see.  :-)
If you can post the code (HTML + JS) that you attempted and the exact errors that resulted, maybe I can help...

 

by: shwaqar82Posted on 2007-06-27 at 05:56:26ID: 19371738

thats the prob. i even cant see what is the error ...but anyways i can give u the code and im sure that u can figure it out whats the error and how to fix it ..

<%@ Page language="VB" Inherits="SOV.CommonTemplate" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<asp:Literal id="metaTitle" Visible="False" Text="Gateway Cities" runat="server"/>
<meta name="description" content="">
<meta name="keywords" content="">
<asp:PlaceHolder id="metaData" runat="server"/>
<link href="/includes/vas.css" rel="stylesheet" type="text/css">
<script type="text/JavaScript">
<!--
var scriptElem = document.getElementById("myScript");
scriptElem.src = '/shared/data/top3/Quebec.js';
var city='toronto_ON';
var scrollerheight="210px";
// -->
</script>
</head>
<body>
<asp:PlaceHolder id="pageHeader" runat="server" />
<div id="content">
<script id="myScript" type="text/javascript" src="/shared/data/top3/Toronto.js"</script>
<form name="city_selector" method="post" action="#">
</form>
</div>
<asp:PlaceHolder id="pageFooter" runat="server" />
</body>
</html>

and below is the code behined:
Imports Microsoft.VisualBasic
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace SOV
Public Class Homepage
      Inherits SOV.CommonTemplate

Private Sub Page_Load
End Sub
End Class
End Namespace

thanks for your great help

 

by: ShimonYKPosted on 2007-06-27 at 06:52:11ID: 19372218

Since this is an aspx page, use asp.net to do what it does well, rather than trying to fake it with javascript.

replace this part

                <script id="myScript" type="text/javascript" src="/shared/data/top3/Toronto.js"</script>

with

                <asp:Literal ID="Literal1" runat="server" Text="<script type='text/javascript' src='/shared/data/top3/Toronto.js'></script>" ></asp:literal>

Add
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
                                <asp:ListItem>Toronto</asp:ListItem>
                                <asp:ListItem>Ontario</asp:ListItem>
                </asp:DropDownList>


for the dropdownlist event handler put this code

                    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
                        Select Case DropDownList1.SelectedItem.Text
                            Case "Toronto"
                                Literal1.Text = "<script type='text/javascript' src='/shared/data/top3/Toronto.js'></script>"
                            Case "Ontario"
                                Literal1.Text = "<script type='text/javascript' src='/shared/data/top3/Ontario.js'></script>"
                        End Select
                    End Sub

 

by: shwaqar82Posted on 2007-06-27 at 06:58:04ID: 19372273

Shimon
thanks for your great help ...my whole effort this time is to save the page go back to the server....if ur snippet works without AutoPostBack i will prefer to do it your way

Any suggestion.......?

 

by: ShimonYKPosted on 2007-06-27 at 07:00:59ID: 19372301

wont work without autopostback.  If you want to actually do anything, you will have to post back to the server.  You can however use an ajax update panel so only the dropdownlist and literal make the postback.

 

by: shwaqar82Posted on 2007-06-27 at 07:12:02ID: 19372392

if dats possible with ajax ..i need your help to implement dat way....im newbie in ajax

 

by: ShimonYKPosted on 2007-06-27 at 07:27:58ID: 19372545

first, install ajax on the web server:
http://ajax.asp.net/


then on the page, right after the form tag add
        <asp:ScriptManager ID="ScriptManager1" runat="server" />

Then wrap this around the DDL and literal
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
               
                </ContentTemplate>
                </asp:UpdatePanel>

Ajax sounds complex at first, but once you get it installed, it is actually very simple

 

by: shwaqar82Posted on 2007-06-27 at 07:32:10ID: 19372578

so in this way only the wrap asp. net control will go back to the server ... is that right........?

 

by: ShimonYKPosted on 2007-06-27 at 08:00:19ID: 19372865

correct

 

by: ThogekPosted on 2007-06-27 at 12:21:18ID: 19375344

AJAX may very well get you want you're looking for, depending on what new script you're trying to load.  The AJAX call goes back to the server, but it doesn't require the page itself to do so, so it's very similar to what you were experimenting with before.  (OTOH, I'm always a little wary of the Golden Hammer syndrome, especially with ASP.NET...)

I'm a little perplexed by your code posting above (06.27.2007 at 05:56AM PDT).
1. You're missing a close-bracket with the line
    <script id="myScript" type="text/javascript" src="/shared/data/top3/Toronto.js"</script>
which should be
    <script id="myScript" type="text/javascript" src="/shared/data/top3/Toronto.js"></script>

2. Your JavaScript code above is running in-line and before the myScript element appears, which means that when the script runs, the element doesn't exist yet, so it won't find it.  You could take the
    <script type="text/JavaScript">
    <!--
    var scriptElem = document.getElementById("myScript");
    ...
    </script>
block and move it down below the myScript element, but...  Done this way, you might as well forget the script and just put the new '/shared/data/top3/Quebec.js' URL directly in the <script> tag; there'd be no difference.  (Or place that JS block inside of a function that gets called at a specific time, such as some other element's onclick event.)

3. That's the method that I didn't really expect to work anyway, but...  Did you try my other suggestion above at all?

This much all makes we want to ask: What exactly are you trying to accomplish here?  What brought up the need to dynamically modify the <script> element's src attribute?  ('Cause I don't see such a need in your code sample above.)

 

by: YZlatPosted on 2007-07-06 at 12:15:30ID: 19434260

try this:

<script type="text/javascript" src='" + <%MyPath%> + "'</script>

and then in your code in a selectchanged event for your dropdown set the value of MyPath variable:

MyPath=myDropdown.SelectedValue

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...