/    Sign up×
Community /Pin to ProfileBookmark

How do I use a var in a src path??

Hi all

Being a relative newbie to javscript (though I understand the syntax thanks to PHP/AS) I don’t really understand how to simply print something out in javascript mid way inside a HTML document.

For example, I have the path to a flash file which requires some dynamic input, i.e. file.swf?var_for_flash=something

I’ve tried setting the flash file path as:

javascript:document.write(‘imgs/nav.swf?var=’+window.myVar)

Have also tried simply setting the flash file path as a javascript function which uses document.write to specify the path, but no joy either..

In short, how can I use a var, established earlier in the page, further down for such a use as this? Hope this makes sense.

Thanks in advance.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisNov 18.2003 — You can place script anywhere necessary in the document (as long as it follows the normal HTML rules). For example:
<i>
</i>&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;script language="JavaScript" type="text/javascript"&gt;
&lt;!--
document.write("&lt;img src='imgs/nav.swf?var=" + window.myVar + "'&gt;") ;
//--&gt;
&lt;/script&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

Does that clear up things for you?
Copy linkTweet thisAlerts:
@mityaauthorNov 18.2003 — Thanks for the help but sadly it doesn't work. Even if I take out the dynamic part of the flash path file, and just put:

document.write('imgs/nav.swf');

It fails.

Bemuses me ? Are there any other conceivable ways in JS to append a file path with a predetermined var?
Copy linkTweet thisAlerts:
@gil_davisNov 18.2003 — I thought I made it plain that you have to write the entire tag, not just part of it.

If you were doing it on the server side, you can stop the flow anywhere and insert script, but on the client side, you have to follow HTML rules. You can't break the tags and you can't embed script inside a tag.

Netscape has a method to include variables, but it is not W3 compliant and no other browser supports it. For example:
Using Inline JavaScript to Position Layers

When using the <LAYER> tag, you can use also inline JavaScript scripted expressions to position the layer. For example, you can position one layer relative to another.

The following example uses inline JavaScript code to define a layer whose ID is suspect1, and then defines another layer whose ID is suspect2 that is positioned 10

pixels below the bottom of the first suspect.

<LAYER ID="suspect1">

<IMG WIDTH=100 HEIGHT=100 SRC="suspect1.jpg">

<P>Name: Al Capone

<P>Residence: Chicago


</LAYER>

<LAYER ID="suspect2"

LEFT=&{"&"};{window.document.suspect1.left};

TOP=&{"&"};{window.document.suspect1.top +

document.suspect1.document.height + 10};>

<IMG WIDTH=100 HEIGHT=100 SRC="suspect2.jpg">

<P>Name: Lucky Luciano

<P>Residence: New York


</LAYER>

Notice these two points in the previous example:

You need to use a semicolon outside the closing curly brace.
You get the value of top from the layer, but you get the value of height from the layer's document.


Although you cannot use inline JavaScript within a style definition for a layer, you CAN use JavaScript to reposition such a layer after it has been defined.[/quote]


I have not been able to find any equivalent methods in IE.
Copy linkTweet thisAlerts:
@mityaauthorNov 18.2003 — Thanks for the clarification. When you said in your first post that it must adhere to standard HTML rules, that didn't translate to me clearly that the whole tag must be re-written, obviously because I'm so used to just slotting PHP in where I like.

I'll give it another go.

Cheers.
Copy linkTweet thisAlerts:
@CharlesNov 18.2003 — [font=georgia]Another, perhaps better way of doing this would be to start with a JavaScript free version, with the IMG element there and the "src" attribute set to a default image for he happy JavaScript free folks. Give the IMG element an "id" and then use something like:[/font]

[font=monospace]<script type="text/javascript">

<!--

onload = function () {document.getELementById('imageId').src = mgs/nav.swf?var=' + myVar}

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@mityaauthorNov 18.2003 — Thanks Charles but gradually throughout this thread we seem to have strayed from flash to images. THe images aren't a problem and the scripts do work for images, but Flash, with its long-winded coding and parameters, doesn't work when outputted by javascript, either in part or in its entirity.

I'll try and fit your method for flash, but I'm not sure it'll work.

Thanks anyway.
Copy linkTweet thisAlerts:
@gil_davisNov 18.2003 — [i]Originally posted by mitya [/i]

[B]Flash, with its long-winded coding and parameters, doesn't work when outputted by javascript, either in part or in its entirity.[/B][/QUOTE]
I just got through helping a guy with writing a whole tag for a flash. It works, if you do it right. Remember to escape the forward slashes and the quotes (or use single quotes).

See http://forums.webdeveloper.com/showthread.php?s=&threadid=21496
×

Success!

Help @mitya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...