Reputation: 11
I have minimal knowlege of javascript so please be kind. I have created a very basic javascript that show a random quote from a politician, who said it, when, and it provides a link to the source of the quote. It looks something like that:
/the quote
var n_text = new Array ();
/the link
var n_lnkk = new Array ();
/the party member that said it and when
var n_part = new Array ();
n_text [1]="blah";
n_text [2]="blah blah";
n_text [3]="blah blah blah";
etc
n_lnkk [1]="http://...";
n_lnkk [2]="http://...";
n_lnkk [3]="http://...";
etc
n_part [1]="whosaidit1";
n_part [2]="whosaidit2";
n_part [3]="whosaidit3";
etc
Then I use math.random and document.write to show a random quote and show a twitter button so that people can tweet it, and a refresh button to show another random quote like this:
blah blah http://... whosaidit and when
I know document.write is not the best way to do it but I have tried other methods and they didn't work with the twitter button so that will have to wait untill I find a solution for it but it is not the question I have right now.
The problem is that this script has become huge, (almost 1000 quotes) so blogger doesn't allow me to add any more text into it. Is there a way to put the data in an external file somewhere and have the javascript read it and display the results and still keep it relatively simple since I have very basic knowledge of jscript or html? Please keep in mind that I am using blogger so I have limited options, but I think an external file with just the data would work
Upvotes: 0
Views: 2951
Reputation: 11
I think I have found a simple and practical solution. An iFrame
did it.
I hosted my script on a free server and then used an iFrame
to get it on my blog. It was the first answer I got, but it seems to have been deleted since.
Upvotes: 1
Reputation: 11171
Yes, you can put the data in an external file. I would suggest a JSON file.
What you'll want to do is either call the JSON file as such:
$.getJSON("link/to/json/file.json", variableOrFunctionNameHere);
OR
$.getJSON("link/to/json/file.json", function(d){...logic here...});
//the d is the data from the file.
However, this might not always work, so I'd look into this question for how to load it non-asynchronously.
Now, to use the JSON file, you'll need to learn some notation.
Let's say you set the JSON file up like this:
[
{
"name" : "Chris Christie",
"quote": "I closed the bridge!",
"date" : "February 1, 2014",
"link" : "https://somelinktosomewhere.com"
},
{
"name" : "Hillary Clinton",
"quote": "Vote for me if I run next election!",
"date" : "March 31, 2014",
"link" : "https://somelinktosomewhere.com"
},
]
and that the JSON file gets loaded in the variable called myVariable
. Then, to access it, you'd find a random number within the size of the JSON file (each selection within brackets is an object) and then myVariable[randomNumber].name
would access the name attribute of the file. So, for example, myVariable[0].name
would return the String "Chris Christie"
. JSON files are really powerful and easy to use.
Upvotes: 1
Reputation: 320
It would be really easy actually, just copy and paste your arrays and what you set them as into a separate .js file. Only copy up to where you have your document.write. Then include it in your tag...
<script src="http://HostedSomewhere/Quotes.js"></script>
Make sure that is included BEFORE your old code.
Upvotes: 1