Jump to content

Reference values from a separated </script> block

venomtail

So I have JS code at the top of the page mixed in with HTML. After the HTML is done I have a button with a separate <script> opening as it doesn't like code to be above it. Looks something like this:

<script>
declare variables
var examplevar = 100;
all functions
</script>

<button>on clic= Dofunctiononclick()</button

<script>
function Dofunctiononclick(){
alert(examplevar);
}
</script>

but now that the script block was broken apart it's unable to reference what examplevar is, it's undefined because it's in a separate block above my button.

 

A super simple fix, an amateur mistake but it's been such a long day my brain doesn't function anymore.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

that doesn't seem right to me ... all the variable declarations in <script> </script> blocks should be global... so one script block should be able to access variables from other blocks... see basic example JSFiddle - Code Playground

 

The variable you set as global may have been reset or overwritten by other functions

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, mariushm said:

that doesn't seem right to me ... all the variable declarations in <script> </script> blocks should be global... so one script block should be able to access variables from other blocks... see basic example JSFiddle - Code Playground

 

The variable you set as global may have been reset or overwritten by other functions

This. It should just work. Another possibility is that you assigned the variable in a scope you overlooked. If it's inside another scope it won't be accessible when returning to the global scope in a new script block.

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

On 9/30/2022 at 4:47 PM, BobVonBob said:

This. It should just work. Another possibility is that you assigned the variable in a scope you overlooked. If it's inside another scope it won't be accessible when returning to the global scope in a new script block.

On 9/30/2022 at 4:40 PM, mariushm said:

that doesn't seem right to me ... all the variable declarations in <script> </script> blocks should be global... so one script block should be able to access variables from other blocks... see basic example JSFiddle - Code Playground

 

The variable you set as global may have been reset or overwritten by other functions

Looks like I wasn't detailed enough. Maybe the extra details might be key

 

I have a blank page. I have data that loads separatley from different sources and once everything is completed, I compile it and load it as simple code. Page itself doesn't need to be that complicated so I can get away with basic HTML text. How it works would be:

...
$( document ).ready() // Right at the top. Maybe that only plays nice with things in its block only not everything in the file?
var dataArray(); // Has data, was filled out before this part of the script
var completeText = "";

completeText = preMadeTags1+preMadeTags2+preMadeTags3+'<button onclick="'+jsFunction(dataArray)+'"></button>'+endTags1+endTags...	;

// Can't remember off the top of my head if insert, replace, amend or something else it's late. Just your general JS HTML replace/insert new text, structure etc...
document.replace("InsertHere") = completeText; // Something along those lines
</script>

HTML stuff
<form>
  <div id="InsertHere">
  </div>
  <script>
  jsFunction(array){  
  alert(array[0]);
  }  
  </script>
...

And no matter what I do the code that is after the HTML stuff it doesn't acknowledge the existance of variables or functions outside of it's own <script> block. Tried specifying that variables are global and so on but nothing works. Maybe I'm injecting the references to the functions incorrectly? I'd hate to have to redo this in jQuery as I'm THAT close and don't wanna rework it all.

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, mariushm said:

$(document).ready() is most likey a jquery thing : https://learn.jquery.com/using-jquery-core/document-ready/

 

the function between the ready() is queued to run / execute when the document completes. But what you put there in ready() is parsed as the code is read.

 

 

So you think the document ready is preventing the code from running because the code was inserted runitme after it decleared it all ready?

Desktop: Ryzen 7 5800X3D - Kraken X62 Rev 2 - STRIX X470-I - 3600MHz 32GB Kingston Fury - 250GB 970 Evo boot - 2x 500GB 860 Evo - 1TB P3 - 4TB HDD - RX6800 - RMx 750 W 80+ Gold - Manta - Silent Wings Pro 4's enjoyer

SetupZowie XL2740 27.0" 240hz - Roccat Burt Pro Corsair K70 LUX browns - PC38X - Mackie CR5X's

Current build on PCPartPicker

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×