Jump to content

Variable Link in JavaScripts


kareem_elzeiny

Recommended Posts

Hello Guys,If javascript source contains a static & a variable PartsLike:http://dstats.net/dstatsjs.php?file=and delineate(link)I've tried to write the code like that

<script type='text/javascript' src='http://dstats.net/dstatsjs.php?file=' + delineate(link)></script>

but it doesn't work ,, Anybody could show me the right way to write this code ??Regards,,

Link to comment
Share on other sites

are you saying your script file source is generated from a query string URL? :)perhaps you could elaborate more on what you're trying to do?

Link to comment
Share on other sites

There is a site which gives a script which count how many times the link has been downloaded The normal code of the script is

<script type='text/javascript'src='http://dstats.net/dstatsjs.php?file=Link'></script>

But I don't have a Static Link here,, My Link depends on variable passing Link

delineate(link)

This is what's it all about Any Idea about how can I write this code ??

Link to comment
Share on other sites

you could do something like thishttp://www.hunlock.com/blogs/Howto_Dynamic...ascript_And_CSSor just use AJAX to make a direct request to

http://dstats.net/dstatsjs.php

and add the variable file name as a query string like in your example

Link to comment
Share on other sites

I've found a new code But I can't Implement it

<script type="text/javascript"><!--var elementfunction loaded() {if (element != null) {document.getElementById('scriptid').removeElement};element = document.createElement("script");element.src = 'dynamic source information goes here';element.type = "text/javascript";element.id = "scriptid";document.getElementsByTagName("head")[0].appendChild(element);}//--></script>

I don't understand what to do with " Scriptid " , "head", "script"can you help me with this code ??

Link to comment
Share on other sites

what did you try? what happened when you tried it? i don't think you need scriptid. did you call the function? please post the code you used when you tried the example.

Link to comment
Share on other sites

<script type="text/javascript"><!--var elementfunction loaded() {if (element != null) {document.getElementById('scriptid').removeElement};element = document.createElement("http://dstats.net/dstatsjs.php?file=");element.src = 'delineate(link)';element.type = "text/javascript";element.id = "scriptid";document.getElementsByTagName("head")[0].appendChild(element);}//--></script>

Link to comment
Share on other sites

Try following the example. You're putting the src string in place of script when doing document.createElement. And if delineate is a function returning something, then you shouldn't be putting it in quotes, because then it's just going to be treated as a string.edit: you should be checking for errors in the console of whatever browser you are using as well.

Link to comment
Share on other sites

you're definitely not following the example very closely...

element = document.createElement("script");   // this shouldn't change.  it's creating a script tagelement.src = 'dynamic source information goes here';  //pretty obvious, should be your baseUrl + dilineate(link), i.e. your full path + link goes here.  It's the srcelement.type = "text/javascript";  // set's the type.  also don't changeelement.id = "scriptid";  //possibly optional, I've never set a script.id be

Link to comment
Share on other sites

I'm using Firefox 4 But I can't find any JavaScript debugger addon
Tools > Error ConsoleYou have to scroll all the way to the bottom.-------------------------------Or look in my sig for the link to FireBug. It's a very powerful addon for FireFox that any serious developer should have.
Link to comment
Share on other sites

you're definitely not following the example very closely...
element = document.createElement("script");   // this shouldn't change.  it's creating a script tagelement.src = 'dynamic source information goes here';  //pretty obvious, should be your baseUrl + dilineate(link), i.e. your full path + link goes here.  It's the srcelement.type = "text/javascript";  // set's the type.  also don't changeelement.id = "scriptid";  //possibly optional, I've never set a script.id be

I've tried but it didn't work
<script type="text/javascript"><!--var elementfunction loaded() {if (element != null) {document.getElementById('scriptid').removeElement};element = document.createElement("script");element.src = 'http://dstats.net/dstatsjs.php?file=' + dilineate(link);element.type = "text/javascript";element.id = "scriptid";//--></script>

the source of the above scripthttp://forums.mozillazine.org/viewtopic.php?f=25&t=48738thanks anyway,, Do you have any idea to make this code ??

Link to comment
Share on other sites

Tools > Error ConsoleYou have to scroll all the way to the bottom.-------------------------------Or look in my sig for the link to FireBug. It's a very powerful addon for FireFox that any serious developer should have.
Thanks for the tip ,,I'm not seeking to be a developer or a programmer, I'm just trying to learn how to fix my personal blog problems by myself.Anyway How can I make a benefit from this tool ??
Link to comment
Share on other sites

any errors? are you checking? From what you should have a closing } because this is all within the block of a function. What about link, where are you getting that from? I don't see anything from the location link I posted about getting the value passed from the other script. Also, you are putting all the code in a function, are you executing the function? What is dilineate()? You be adding some basic alerts to make sure everything is what you expect. I have a feeling you are just assuming that everything is doing what you want without really testing for anything.i.e.

<script type="text/javascript"><!--var element = {};function loaded() {  alert('ENTER loaded');  if (element != null) {document.getElementById('scriptid').removeElement};	var baseUrl = 'http://dstats.net/dstatsjs.php?file=';	var link = dilineate(link);	var src = baseUrl + link;		alert('SRC IS => ' + src);	element = document.createElement("script");	element.src = src;	element.type = "text/javascript";	element.id = "scriptid";};loaded();//-->

Link to comment
Share on other sites

any errors? are you checking? From what you should have a closing } because this is all within the block of a function. What about link, where are you getting that from? I don't see anything from the location link I posted about getting the value passed from the other script. Also, you are putting all the code in a function, are you executing the function? What is dilineate()? You be adding some basic alerts to make sure everything is what you expect. I have a feeling you are just assuming that everything is doing what you want without really testing for anything.
Sir, I didn't say that I'm an expert in Javascript ,, That's far away from my field of study.I'm just trying to learn how to do this stuff myself & it's hard to understand !!And Sure I'm testing this script myself & I'm trying to do my best to know what's wrong with it !!I've tested the script you provided above but only the first alert appeared & it didn't work.
Link to comment
Share on other sites

Hey don't take it personally, I'm just trying to get you to learn how to try and figure some of these simple things out on your own so you can better help yourself. We've mentioned error checking before. alerts or console logging are other simple way's the help one help themselves when writing code.so... are you getting any errors?

Link to comment
Share on other sites

Ok it's alright, thank you for helping :)I got 2 errors

Error: document.getElementById("scriptid") is nullLine: 477

Error: dsCounter is not definedLine: 495

Edit: dsCounter depend on the script src itself since the original script is

<script type='text/javascript' src='http://dstats.net/dstatsjs.php?file=link'></script><script type='text/javascript'> document.write(dsCounter+'x');</script>

So I think that if we were able to load the script src this error will disappear ,, Right ??

Link to comment
Share on other sites

hmm, you can take probably take out these lines probably,

if (element != null) {document.getElementById('scriptid').removeElement}..element.id = "scriptid";

it looks like its just making sure to get rid of any pre-existing script tags.I'm not sure about the second error because I'm not sure what the context is regarding what we've been discussing. is this page live somewhere? what is dsCounter? is that what comes back?edit: what does the alert for the url look like? can you post that?

Link to comment
Share on other sites

Everything is related. This is what is causing the error:

function loaded() {  alert("ENTER loaded");  var baseUrl = "http://dstats.net/dstatsjs.php?file=";  var link = delineate(link);

You're calling delineate with a variable called link, but link doesn't have a value. That's why you get the error message that the variable is undefined.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...