John J. Hughes II
10/2/2015 10:36:00 AM
On 02/10/2015 08:44, Evertjan. wrote:
>> <script type="text/javascript">
>> function doIt() {
> This does not work, as doIt() is not called clientside.
What do you mean by "does not work"? I have this code and it does open
the index.html when this doIt is called. The purpose of doIt is to
prepare a string (some_string) and then open index.html. And index.html
contains js code and the purpose is that this js could somehow use this
some_string content. yes its a local variable here and is deleted, but I
mean we could pass it or put on some global variable to used it later in
that javascript file. Thats the purpose...
>
>> var some_string = <?php echo json_encode($sometext); ?>;
> This is totally errorprone, you need to enclose the litteral string in '-s
> or "-s, ensuring that the ones used do not appear in the inner text.
yes true.
>
> Have a look houw the resulting stream looks on the client-browser using
> view-source.
>
>> window.location.href = encodeURI("index.html");
> There is no need to encodeURI() "index.html"'
well, there was other code there as well, like variables, so I needed
that encode.... but yes not here is needed.
>
> If[!] doIt() is run then the clientside variable some_string is lost.
>
yes, but the question is that where can we save that some_string (or
pass it?) so that later on some javascript file could us it. Is a good
idea to pass it to index.html somehow and from there to that javascript
file?
>> Then this html site calls javascript
>> function from a js-file:
> No.
>
> html-PAGES do not CALL javascript files,
> they could include code to include a js-file.
yes and that code calls a js function. thats what happens
>
>> test.js :
>>
>> function doSomething() {
>> // here I want to use that some_string created in the php file....
>> }
> That variable some_string, being a clientside javascript varaiable on
> another PAGE, was lost when leaving that page.
yes, but can we save it somehow somewhere to be able to use here?? thats
the question. or can we have some route to pass it all the way here from
there?
>
>> So in doSomething I want to be able to use this some_string created in
>> php file. How can I do it?
> So you cannot.
>
> Why don't you create that string on the "index.html" PAGE,
hmmm, yes i could possibly create it in that index.html (although to
create it i need php code... but I guess its doable in html file....
surely) page (as I can pass information to that file from that php file
to form that string). But how could this help?
> that you probably will have to rename to "index.php".
yes, that is a good idea.... at least to try. and migh work for me...
but index.html is quite big file I think, i hope the change happens
without problems. I can try....
>
> I tried couple of things, but I do not really
>> understand Ajax...
> There is no mention of Ajax here, only of some lost serverside json-code.
not here , but I tried Ajax elsewhere
> :) ?
>
> Being a newbie is no joke, it is and should be hard.
>
> Seems to me you need to explain what you want in a better way.
>
> Programming primarily is about describing what you want to yourself in a
> logical way, not copying code from the web you do not understand and
> expecting it to do what you have not visualized.
so far I pretty much understand what is happening and the sites work ok
.. But am a bit stuck with this one. I can pass php variables to js code
in a same file, no problems.
>
> And remember, only Javascript is on topic here, either clientside or
> serverside, php is not.
ok but those languages are very much linked in real life....
>> Re: How to pass a php variable to a javascript file (function)?
> Seems you were talking about a clientside Javascript variable here,
I have php file with contains this js code which creates this variable I
want to forward to other javascript files:
php-file:
- contains a js-code which creates a js variable (and/or php variable...
does not matter)
index.html:
- php-file opens this
- this file contains a js code/fucntion which is located in a separate
js-file
js-file:
here, in one function, I want to get hold of that string which was
created in php file above.
> those variables are only valid as long as the PAGE exists,
> they do not exist on the [serverside] SITE.
>
> Your "javascript file" surely exists on the server, so including something
> there needs serverside scripting. You could howeve include something in the
> stream that is sent to the client using php when renaming the js file to:
> "myJSfile.js.php".
>
ok thanks, at least I can try your idea of changing that index.html to
php... maybe it works...