I am trying to create a SVG (actually created by a svg.php
-file) with an external .js-file. When putting all the javascript functions directly in the SVG it works properly:
<?php
header('Content-type: image/svg+xml');
?>
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
width="800"
height="600"
id="example_text">
<g id="layer1">
<text
x="100"
y="100"
id="text_holder"
style="font-size: 12pt;">
<?php echo filter_input(INPUT_GET, 'text'); ?>
</text>
</g>
<script type="text/javascript">
function create_from_rect (client_rect, offset_px) {
if (! offset_px) {
offset_px=0;
}
var box = document.createElementNS(
document.rootElement.namespaceURI,
'rect'
);
if (client_rect) {
box.setAttribute('x', client_rect.left - offset_px);
box.setAttribute('y', client_rect.top - offset_px);
box.setAttribute('width', client_rect.width + offset_px * 2);
box.setAttribute('height', client_rect.height + offset_px * 2);
}
return box;
}
function add_bounding_box (text_id, padding) {
var text_elem = document.getElementById(text_id);
if (text_elem) {
var f = text_elem.getClientRects();
if (f) {
var bbox = create_from_rect(f[0], padding);
bbox.setAttribute(
'style',
'fill: none;'+
'stroke: black;'+
'stroke-width: 0.5px;'
);
text_elem.parentNode.appendChild(bbox);
}
}
}
add_bounding_box('text_holder', 10);
</script>
</svg>
Can be loaded with e.g. svg.php?text=Test
(Which creates an SVG and draws a box around it. The example is mainly taken from How can I draw a box around text with SVG?.)
However, I would like to put the javascript functions in an external .js-file. Unfortunately, I cannot figure out where to load it. Putting the functions in svg.js
and adding a simple <script src="svg.js"></script>
before the other script in the php-file seems not do to the trick.