Jump to content

change the source of the javascript function?


dadamssg

Recommended Posts

Ok..this is kind of complicated. I wrote a php script that spits out a calendar in javascript. I use it as the source of a javascript function. heres the javascript code that will be placed in a html webpage

 

<script type="text/javascript" src="http://localhost:8888/calendars/calendar_javascript.php?m=11&y=2010"></script>

 

as you can see the script uses the $_GET['m'] and $_GET['y'] to determine which month to display.

 

Again it all works but i want to have the script produce Next and Previous month links. and then when clicked it would change the source of the script to have different m & y values.

 

I kinda doubt this is possible but can anyone recommend a way to achieve this?

Link to comment
Share on other sites

The browser won't make another HTTP request to the script just because you change the src attribute of the script tag. This is something that should be contained within a function and bound to an event:

 

<script type="text/javascript" src="http://localhost:8888/calendars/calendar_javascript.js"></script>
<script type="text/javascript">
window.onload = function() {
    document.getElementById('calendarPopButton').onclick = function() {
        popCalendar(this);
    }
}
</script>

 

The external script would contain the 'popCalendar' function. Passing 'this' to it would pass the 'calendarPopButton' element, for uses in positioning and such. This way you have a re-usable function, that can be easily updated through other functions (i.e. 'setCalendarMonth()' or something).

 

Having said all that; jQuery has a powerful, yet simple datepicker widget available within the UI library that would make this a thousand times easier for you.

Link to comment
Share on other sites

hey thanks...i figured out a way to do what i want. I have a javascript that has a php file as its source to spit out javascript. Then i have a function in the head that updates the div with a php script that spits out html.

 

<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>

<style type="text/css">
    body{
          background: url(http://localhost:8888/css/lightgreenstripe.png);		
        }
</style>

	<script type="text/javascript">
	function ChangeMonth(X,Y){
	var changemonthlink = "http://localhost:8888/calendars/calendar_html.php?m=" + X + "&y=" + Y;
	$("#relEVENTz").load(changemonthlink);
	};
	</script>

<link type="text/css" href="http://localhost:8888/css/dynamic.php?color=blue" rel="stylesheet" />	
</head>
<body>
<br>

<div id="relEVENTz">
<script type="text/javascript" src="http://localhost:8888/calendars/calendar_javascript.php?m=11&y=2010"></script>
</div>

</body>
</html>

Link to comment
Share on other sites

Well, to answer the question originally posed - it is absolutely possible to change the source of an included javascript source file. Done this before. You just use javascript to change the src attribute for the script object. The easiest way to do this is to include an ID in the script tag.

 

Here are a few test files to illustrate how it works.

 

html document:

<html>
<head>
<script id="jsInclude" type="text/javascript" src="one.js"></script>
<script type="text/javascript">
function changeJS(jsFile)
{
  document.getElementById('jsInclude').src = jsFile;
}
</script>
</head>
<body>
<button onclick="changeJS('one.js');">Use JS1 Include</button>
<button onclick="changeJS('two.js');">Use JS2 Include</button>
<br /><br />
<button onclick="test();">Run the test function</button>
</body>
</html>

 

JS source file one.js

function test()
{
  alert('This is test in include 1.');
}

 

JS source file two.js

function test()
{
  alert('This is test in include 2.');
}

Link to comment
Share on other sites

mjdamato I think you'll find that doesn't work in FF, Chrome or Safari.

 

No, I won't find that. I only have IE on my work PC and I refuse to test it at home on the basis that I might have to admit there is a problem with the code I submitted.

I reject your reality and substitute my own.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.