JavaScript in front-end pages

It seems for me that by wix design i can not use JavaScript regularly, for example if i write in the page script document or window it will tell me these variables are undefined ? is there anyway to go around this issue?

And is there anyway to inject custom scripts in the front-end?

I have also noticed that afterGet and beforeGet hooks are not firing at all?

Last thing
export function entries_afterQuery(item, context) {
console.log(item);
item.description = ‘test’;
item._createdDate = ‘3/6/20’;
console.log(item);
return item;
}

the afterQuery is firing and i can see item is indeed changed however it seems that results are returned to front-end before the change actually happens - meaning the above code does not have any effect on the items that are returned to front-end is this the expected behavior?

#Rawabi

Hi,

Wix code does not expose Window and Document. To interact with page elements use the $w scope, as explained here

You can find out more about what is and isn’t possible in this article
In general, it is not possible to import modules.

The beforeGet and afterGet hooks should work as expected.
Note that you must return the item (or itemId, for the beforeGet hook) in order for the hook to execute.

The afterQuery hook only modifies the returned items, and is executed for each item.
The collection remains unaffected by this hook.

in the afterGet i have returned the item, but for me the whole function is not getting executed in the first place

here is my code, and ‘test’ does not get logged at all
export function entries_afterGet(item, context) {
console.log(‘test’);
item._createdDate = ‘3/6/20’;
return item;
}

is there anyway to inject custom JavaScript into front-end pages?

Hi,

Did you set the hook by going to the ‘entries’ collection and clicking ‘Add hook’ ?
Typing the hook code in the data.js is not enough to register it.

I added the hook by using the add hook link which generated this function for me , i just filled it with the code above.