I am developing a Javascript widget for other websites to consume. I have 2 javascripts and one php file. One Javascript for user to consume and other to make a JSON request and the php to digg the data. Things are working great and data is being displayed, as well.
Now I need to add CSS to the widget. I don't want the users to code the CSS in their side. How do I do it? Do I need code all inline css? or Append a css file in the javascript?
I know this is very basic. But, I am not familiar with this stuff. So, if you guys point me to the right direction that would be great. I need the sample code as well. I checked many site. They simply have the javascript code.
Thanks!
End user Javascript
------------------------
<script type="text/javascript" src="http://mydomain.com/userwidget.js"></script>
<div id="results">
</div>
<script type="text/javascript">
ajax_get_my_data();
</script>
AJAX Javascripit
----------------
function ajax_get_my_data(){
var results = document.getElementById("results");
var hr = new XMLHttpRequest();
hr.open("POST", "http://mydomain.com/widgets/userwidgetcore.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var data = JSON.parse(hr.responseText);
results.innerHTML = "";
for(var obj in data){
results.innerHTML += data[obj].propertyA;
}
}
}
hr.send("var1=stock&var2=up");
results.innerHTML = "requesting...";
}
PHP code
------------
<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
...........
Databse logic
.......
$_out = '<table id=\"rounded-corner\">';
$_out .= '<thead>';
$_out .= '<tr>';
$_out .= '<th colspan=\"3\" scope=\"col\" class=\"rounded-q6\">Stocks Under Accumulation</th>';
$_out .= '</tr>';
$_out .= '<tr>';
$_out .= '<th scope=\"col\" class=\"rounded-q5\">Ticker</th>';
$_out .= '<th scope=\"col\" class=\"rounded-q5\">Price % Change</th>';
$_out .= '<th scope=\"col\" class=\"rounded-q5\">Volume % Change</th>';
$_out .= '</tr>';
$_out .= '</thead>';
$_out .= '<tbody>';
..............
..................
$jsonData = $_out;
echo $jsonData;
?>
now I need to load the css to the tags. How do I do it?