Solved

Simple Javascript question:

Posted on 2015-02-18
4
81 Views
Last Modified: 2015-02-18
Here is the entire code I have for my site:

 
<script defer="defer" src = "test.js">
console.log("Test 1");        
setOptions({
		UseFooter: true,
		UseHeader: true,
		Classification: "None",
		Style: "default.css"
	});
console.log("Test 2");
    </script>

Open in new window


Inside of "test.js" is just a simple "console.log("Test 3");" and a setOptions function that takes 1 variable.  In that function is a console.log("Test 4");

What I would EXPECT to happen:  

For the console to show :
Test 1
Test 2
Test 3
Test 4

(Or some variant of that in some order).  

Instead, all I get is "Test 3".  I assume I'm doing something wrong in the actual script code here.
0
Comment
Question by:Javin007
  • 2
  • 2
4 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,
this will not work as the inline script will never get executed as you have both inline script and an external reference (src).
Just use this and it should work as expected:
<script defer="defer" src = "test.js"></script>
<script>
console.log("Test 1");        
setOptions({
		UseFooter: true,
		UseHeader: true,
		Classification: "None",
		Style: "default.css"
	});
console.log("Test 2");
    </script>

Open in new window

See also an explanation:
http://ejohn.org/blog/degrading-script-tags/

HTH
Rainer
0
 
LVL 4

Author Comment

by:Javin007
Comment Utility
Yeah, I kinda figured that was the problem, so tried it your recommended way. Unfortunately, that way, it doesn't seem to recognize that "setOptions()" is an existing function (presumably because it hasn't actually loaded yet).  

What's the "correct" way to pass parameters to a Javascript plugin?
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
Comment Utility
Hi,
simply remove the defer attribute, then this should work.
Normally you use events like body onload etc to execute Javascript after all resources have been loaded.
Personally I use the jQuery framework a lot as it encapsulates some of the important events.
<script src="test.js"></script>
<script>
console.log("Test 1");        
setOptions({
		UseFooter: true,
		UseHeader: true,
		Classification: "None",
		Style: "default.css"
	});
console.log("Test 2");
    </script>

Open in new window

HTH
Rainer
0
 
LVL 4

Author Closing Comment

by:Javin007
Comment Utility
Ah, thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now