0

I'm building a Blog with Html/Css/JQuery and I've hit a wall.

I want to load a different Blog Post based on the Number passed in from the link beforehand. For example, if I have 3 Posts, load content based on which number is selected.

This is in content.js.

var prepareContent = function (postNumber) {
     readTextFile(postNumber);
};

However, I have no clue how to manipulate postNumber when click is called in another .js file.

This is in index.js.

$("#post0").click(function () {
    conentPassed = 0;
    window.location = "content.html";

});

$("#post1").click(function () {
    contentPassed = 1;
    window.location = "content.html";
});

I would like to "pass" a variable into content.js from index.js. Is there any way to do this? Thank you.

Steven
  • 604
  • 6
  • 11
  • Similar questions: [JavaScript: Two separate scripts - share variables?](http://stackoverflow.com/questions/8348401/javascript-two-separate-scripts-share-variables) # [Global variables in Javascript across multiple files](http://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files) # [Sharing JS variables in multiple – ThisClark Sep 13 '16 at 22:46
  • I looked at those questions and it only seems to work if I use both JQuery scripts in one Html file. Extending the question, is there any way to manipulate the variable if each script is attached to different Html files? – Steven Sep 13 '16 at 23:11
  • Use local or sesssion storage to store the variable and then call it from local / session storage in each of the pages and pass it into the relevant functions – gavgrif Sep 13 '16 at 23:16
  • what are `$("#post0")` and `$("#post1")`? Sure looks like you are trying to reproduce `` tags that would do what you need with less complication – charlietfl Sep 13 '16 at 23:43

1 Answers1

0

I think as ThisClark has pointed out there are similar problem-solutions.

I had a similar problem a while back and found that a hash addition to the url helped:

window.location = "content.html/#" + contentPassed;

then on the receiving end:

postNumber = window.location.href.split("#")[1];
postNumber = Number(postNumber);

this gets the current url, then splits it into parts using the hash as the splitter, the parts become an array, and the [1] gets you the second part. The second line is to be sure it's no longer a string.

don't know if this helps?

Sam0
  • 1,459
  • 1
  • 11
  • 13
  • I'll try it out! Thanks :). – Steven Sep 13 '16 at 23:19
  • forgot: your outgoing line should be window.location.href = "conte.... (add the href to go there) – Sam0 Sep 13 '16 at 23:26
  • Do you have any idea why my attached Resource files aren't loading properly when I load "content.html/#0"? It loads fine when it's simply "content.html" – Steven Sep 13 '16 at 23:30
  • seems your path for the resources is relying on the url, I didn't come across this problem, can only suggest fixing the resource file paths. however, if this isn't an option then perhaps this method is a no go? sorry. or can you tell me how your resources are referenced? do you mean scripts or image files (or both?) – Sam0 Sep 13 '16 at 23:53
  • It's my Scripts and StyleSheets giving me the error. – Steven Sep 13 '16 at 23:54
  • is the path to these scripts set by the url? consider using the query string method i.e. a `?` instead of a `#` . if you have images and they work then the try to ensure you're using the same path/src describing method for your scripts? – Sam0 Sep 13 '16 at 23:59
  • The path of these scripts are set in the Html file. – Steven Sep 14 '16 at 00:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123297/discussion-between-sam0-and-steven). – Sam0 Sep 14 '16 at 00:14
  • Got it to work. Used link as "content.html?post=0" and used that parameter. Thank you for setting me in right direction. – Steven Sep 14 '16 at 05:12